This article is mainly for everyone to bring an AJAX fast resolution parameter too long can not be submitted success problem. Small series feel very good, now share to everyone, also for everyone to make a reference. Follow the small series together to see it, hope to help everyone.
A lot of data have been said that the Get method parameters are limited, the length of the parameters of the Post method is unrestricted, which is also post compared to the advantage of get has been.
Using the Post method in Ajax, the usual parameter format: PARAM1=A1&PARAM2=A2, when the parameter length is too long, the commit is unsuccessful. For example, we often write an AJAX POST request:
$.ajax ({ type: "POST", //Post or get contentType: "Application/json;charset=utf-8", data: " Requesttag= "+tag+" &content= "+content, //request parameter URL:" Postmockjson ", //Address DataType:" Text ", error:function (Err) { outlog ("error" +err); }, success:onsavesuccess});
When this is used, it is found that if the parameters 2:content too much content, for example, I pass a relatively large text content, I am in the background service (I use the servlet) when obtained:
String content= request.getparameter ("content");
The value of the content here, is null.
There is also a quick way to see if the AJAX request is successful, debug with the F12 Developer tool, and, after executing the AJAX code, in the Network Options page in the F12 tool, you can see the request that was initiated, and the parameters of the request that you see are incorrectly prompted.
Workaround:
The AJAX parameter format has another way of writing: The JSON-formatted request parameter, which I can write:
var param = "{requesttag:\" "+requesttag+" \ "content:\" "+content+" \ "}";
(PS: Note that the JSON format is correct)
At this point, if you use F12 for debug, you can see that the data for the requested parameter is correct.
So the question is, the content I get in the servlet is still null, why???
Because the request parameter is a JSON block, this request.getparameter ("content") method, of course, does not get the data, because it does not parse the JSON data for us.
So where do we pass the parameter data?
Here's the point: the data is in the Request object.
Then we will use the most primitive method, through the data flow method to obtain the data passed, as follows:
Request.setcharacterencoding ("UTF-8"); StringBuilder sb = new StringBuilder (); try (BufferedReader reader = Request.getreader ();) {char[] buff = new Char[1024];int len, while (len = reader.read (buff))! =-1) { sb.append (buff,0, Len); }} catch (IOException e) { e.printstacktrace ();}
At this point, our JSON data is in the SB object, and then we just need to parse the JSON object:
Jsonobject jobject = Jsonobject.fromobject (sb.tostring ()); String Requesttag = jobject.getstring ("Requesttag"); String content = jobject.getstring ("content");
Here we can get to the content.