In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces ajax how to solve the problem that the parameters are too long to be submitted successfully, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.
After checking a lot of data, it is said that the parameters of get method are limited, and the length of parameters of post method is unlimited, which is also the advantage of post over get.
Using the post method in ajax, using the regular parameter format: param1=a1¶m2=a2, the submission is still unsuccessful when the parameter length is too long. For example, we often write a post request for ajax 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, it is found that if the parameter 2:content content is too much, for example, if I upload a large text content, when I obtain it from the backend service (I use servlet):
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 to debug. After executing the ajax code, you can see the initiated request on the network options page in the F12 tool. At this time, you can see an error in the parameters of the request.
Solution:
There is another way to write the parameter format of ajax: request parameters in json format, which I can write as follows:
Var param = "{requestTag:\"+ requestTag+"\ ", content:\" + content+ "\"} "
(ps: note that the json format should be correct)
At this time, if you use F12 for debug, you can see that the data of the requested parameters are correct.
So the question is, the content I got in servlet is still null. Why?
Because the request parameter is a json data block, this request.getParameter ("content") method, of course, can't get the data, because it won't parse the json data for us.
So where is the parameter data we pass?
Here's the point: the data is all in the request object.
Then we use the most primitive method to obtain the transmitted data through the method of data flow, 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 all 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")
At this point, we can get content.
Thank you for reading this article carefully. I hope the article "ajax how to solve the problem that the parameters are too long to submit successfully" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.