Ajax quickly solves the problem of too long parameters that cannot be submitted successfully. ajax parameters are too long to be submitted.

Source: Internet
Author: User

Ajax quickly solves the problem of too long parameters that cannot be submitted successfully. ajax parameters are too long to be submitted.

According to many materials, the parameters of the get method are limited, and the length of parameters of the post method is unlimited. This is also the advantage of post over get.

The post method is used in ajax. The conventional parameter format is param1 = a1 & param2 = a2. When the parameter length is too long, the submission fails. For example, we often write an ajax post request like this:

$. 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 using this method, I find that if parameter 2: content is too large, for example, if I transfer a large text content, I will serve it in the background (I use servlet) when getting:

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

The value of content here is null.

There is also a quick way to check whether the ajax request is successful. Use the F12 developer tool for debugging. After the ajax code is executed, on the network option page of the F12 tool, you can see the initiated request. At this time, an error message is displayed for the request parameters.

Solution:

The ajax parameter format also has another Syntax: Request Parameters in json format, which can be written as follows:

Var param = "{requestTag: \" "+ requestTag +" \ ", content: \" "+ content + "\"}";
(Ps: The json format must be correct)

At this time, if F12 is used for debugging, we can see that the request parameter data is correct.

The problem is that the content I get in the servlet is still null. Why ???

Because the request parameter is a json data block, the request. getParameter ("content") method cannot obtain data because it does not parse json data.

So where is the passed parameter data?

The point is: data is stored in the request object.

Then we use the original method to obtain the transmitted data through the data stream method, as shown below:

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 time, our json data is in the sb object. Next we only 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 the content.

The above ajax quick solution to the problem that the parameter cannot be submitted successfully is all the content shared by Alibaba Cloud. I hope you can give us a reference and support for the customer's house.

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.