How does the servlet get data that is passed in JSON in the AJAX format? __js

Source: Internet
Author: User
Tags stringbuffer
$ (' #but_json_json '). Click (function () {
var j ={"name": "King", "Password": 123456};

$.ajax (
{
URL: "Servlet/jsonobject",//Access Path
Type: "POST",//Access mode
DATA:J,//Incoming service-side data
DataType: "JSON",
ContentType: "Application/json;charset=utf-8",
Success:function (data) {
alert (data);
alert (data.name);


alert (Data.password);
}

}
);
});



Servlet:


public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {

String user = Request.getparameter ("J");
String name = Request.getparameter ("name");
String Password = request.getparameter ("password");
SYSTEM.OUT.PRINTLN (user);
JSON object
Jsonobject jsonobject = new Jsonobject ();
Jsonobject.accumulate ("password", password). Accumulate ("name", "www");
Response.setcontenttype ("Application/json");
Response.getwriter (). Write (jsonobject.tostring ());
}




The problem now is that I can parse the data out of the servlet from the client, such as the name=www above, but the object passed in by the client
(Var j ={"name": "King", "Password": 123456}) Not in the servlet, as if to go first to the object, I think it should be,
After all, JSON provides many ways to convert the JSON data you get to another format, but it's still confusing. I am novice, the first contact, want to know the friend appointed, grateful.




For:
That's what I understand. First of all, for the JSON serialization you want to pass to the server (servlet), use Json.stringify,
Make sure that the string we pass conforms to the JSON structure, because the data we pass is passed to the server as streaming, so we should
Server-side read-in buffer in conversion string output
Public String readjsonstring (HttpServletRequest request) {
StringBuffer json = new StringBuffer ();
String line = null;
try {
BufferedReader reader = Request.getreader ();
while (line = Reader.readline ())!= null) {
Json.append (line);
}
}
catch (Exception e) {
System.out.println (E.tostring ());
}
return json.tostring ();
}


And then turn to the Jsonobject object. You can take the value.
There's one more thing to note. encoding format
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.