Using ajax to transmit values in post mode, the received values are different from those transmitted.

Source: Internet
Author: User
On the displayed page, use ajax to post the value. The value data is in json format. Received in the controller, but only received the part score. There is a url in the uploaded value data. The url contains the special symbol & amp;. The received value is the symbol, and the following is missing. For example, data in source code...

On the displayed page, use ajax to post the value. The value data is in json format. Received in the controller, but only received the part score. There is a url in the transmitted value data. The url contains a special symbol &, And the received value is the symbol, which is missing. For example, the data in the source code is the value I want.

Reply content:

On the displayed page, use ajax to post the value. The value data is in json format. Received in the controller, but only received the part score. There is a url in the transmitted value data. The url contains a special symbol &, And the received value is the symbol, which is missing. For example, the data in the source code is the value I want.

Depends on the type you pass to data:

  • If it is an object, jquery will automatically add encodeURIComponent when sending the ajax request, and the & symbol is encoded as % 26, not automatically truncated by formData
  • If data is a string, jquery automatically places the string in formData and sends it to the server. Because formData is passed and truncated, the string is automatically truncated without passing through encodeURIComponent.

Before submission, URL Encode the url you uploaded

The value corresponding to data is encapsulated in JSON. stringify ({}).
For example:

$ (". A_post "). on ("click", function (event) {event. preventDefault (); // invalidate the method that comes with a $. ajax ({type: "POST", url: "url address", contentType: "application/json", data: JSON. stringify ({param1: "param1", param2: "param2", url: "http: // xxxx? A = a & B = B & c = c ", param3:" param3 "}), // dataType:" json ", success: function (result) {// actions after the request is correct}, error: function (result) {// actions after the request fails }});});

View the request parameters as follows:

Use this JSON. stringify ({}) is encapsulated, that is, a problem occurs when ajax is passed. data: 'Data = '+ data is used, so there is a problem with passing the value, but data: {data: data}. That's all. Why?

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.