Ajax Fast resolution parameter too long unable to submit successful issue _ajax related

Source: Internet
Author: User

A lot of data have said that the parameters of the Get method are limited, the length of the parameters of the Post method is unlimited, which is also the advantage of the post compared to got.

Using the Post method in Ajax, the normal parameter format: PARAM1=A1&PARAM2=A2, when the parameter length is too long, still commit 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 used in this way, I find that if the parameter 2:content is too much, for example, I am passing a relatively large text content, I am in the background service (I use a servlet) to obtain:

String content= request.getparameter ("content");

The value of the content here, is null.

There's also a quick way to see if Ajax requests are successful, debug with the F12 developer tool, and after the AJAX code is executed, in the Network Options page of the F12 tool, you can see the request being initiated, when you see the requested parameter with the wrong hint.

Workaround:

The AJAX parameter format also has another way of writing: JSON-formatted request parameters, which I can write:

var param = "{requesttag:\" "+requesttag+" \ ", content:\" "+content+" \ "}";
(PS: Note that the JSON format should be 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, what is the content I get in the servlet is still null???

Since the request parameter is a JSON block, the Request.getparameter ("content") method, of course, does not get the data because it does not parse the JSON data for us.

So where are the parameter data we're passing?

Here's the point: the data is in the Request object.

So we use the original method, the data flow method to get 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");

When we get here, we can get the content.

The above Ajax fast resolution parameters too long can not be submitted to the success of the problem is small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.