Foreground Jquery+ajax+json value, after the background processing to return the JSON string, how to take the property value inside? (No SPRINGMVC annotations)

Source: Internet
Author: User
Tags string to json jqgrid

I. Taking a property value

Front page:

function Select (ID) {
Alert ("HFDFHDFH" +id);
$.ajax ({
URL: "Selectbyid.jsp",
datatype: ' JSON ',
Data:{id:id},//Send data
Error:function (data) {
Alert ("Wrong, no this student!! ");
},
Success:function (data) {
alert (data);
var json = eval ("(" +data+ ")");//Convert JSON type string to JSON object
Alert ("HJF" +json.name);
$ ("#id"). Val (json.id),
$ ("#name"). Val (Json.name),
$ ("#age"). Val (Json.age);
IfJson.sex= = ' man ') {
$ ("Input:radio[name= ' sex ']"). EQ (0). attr ("Checked", true);
$ ("input[name=sex]"). attr ("Checked", "checked");
$ ("#sex"). attr ("Checked", ' checked ');
$ ("#sex"). Removeattr ("checked");
}else{
$ ("Input:radio[name= ' sex ']"). EQ (1). attr ("Checked", true);
$ ("input[name=sex]"). attr ("Checked", "checked");
$ ("#sex"). attr ("Checked", ' checked ');
$ ("#sex"). Removeattr ("checked");
}
}

Background code:

<%@ page language= "java" import= "Java.util.*,com.dao.*,com.bean.*,com.fasterxml.jackson.databind.objectmapper" pageencoding= "Utf-8"%>
<%
int id = integer.parseint (request.getparameter ("id"));
Studentdao dao = new Studentdao ();
Studentbean Bean = Dao.selectbyid (ID);
if (bean!=null) {
Objectmapper x = new Objectmapper ();
String str = x.writevalueasstring (bean); To convert a Java class object to a JSON string
System.out.print (str);
response.getwriter (). print (str); Returning data to the foreground Ajax
}
%>

Two. Front-end Ajax does not receive JSON values what's going on?

Front page:

function Ajaxpost () {
$.ajax ({
data:{"Name": $ ("#name"). Val (),
"Age": $ ("#age"). Val (),
"Sex": $ ("input[name= ' Sex ']:checked"). Val ()},
Type: "Post",
Async:false,//Plus this attribute is good.
DataType: ' JSON ',
URL: "Addstudent.jsp",
Error:function (data) {
Alert (' Add failed! ‘);
},
Success:function (data) {
Alert (' Add success! ‘);
window.location.href = ' list.jsp '
}
});
}

Background code:

<%@ page language= "java" import= "java.util.*,com.dao.*,com.bean.*" pageencoding= "Utf-8"%>
<%
String name = Request.getparameter ("name");
int age = Integer.parseint (Request.getparameter ("Age"));
String sex = request.getparameter ("Sex");
Studentbean student = new Studentbean ();
Student.setname (name);
Student.setage (age);
Student.setsex (Sex);
Studentdao dao = new Studentdao ();
int rows = dao.addstudent (student);
if (rows>0) {
response.getwriter (). Print ("{}");
}
%>

Three. The background query database returns the collection, and the foreground is displayed in the table.

Front page:

$ (document). Ready (function () {
$ ("#list4"). Jqgrid ({//jqgrid Table control
URL: "Selectstudent.jsp",
DataType: "JSON",
Mtype: "POST",
height:600,
PostData: "{searchsql: '}",
Autowidth:true,
colnames:[' ID ', ' name ', ' age ', ' gender ',
colmodel:[
{name: ' id ', index: ' id ', hidden:true},
{name: ' name ', index: ' Name '},
{name: ' Age ', Index: ' Age '},
{name: ' Sex ', index: ' Sex '},

],
Rownumbers:true,
Viewrecords:true,
jsonreader:{
ID: "Blackid",
Repeatitems:false
},
pager:$ (' #gridPager ')
});
});

Background code:

<%@ page language= "java" import= "Java.util.*,com.dao.*,com.bean.*,com.fasterxml.jackson.databind.objectmapper" pageencoding= "Utf-8"%>
<%
Studentdao dao = new Studentdao ();
list<studentbean> list = Dao.selectall ();
if (list!=null) {
Objectmapper x = new Objectmapper ();
String str1 = x.writevalueasstring (list); Collection to JSON type string
Response.getwriter (). print (STR1); Back to front end Ajax
}
%>

Foreground Jquery+ajax+json value, after the background processing to return the JSON string, how to take the property value inside? (No SPRINGMVC annotations)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.