Using AJAX to transmit values in post, the value received is not the same as the transmission

Source: Internet
Author: User
Tags truncated

In the display page, using AJAX to post the value of the value data is in JSON format. Received in the controller, but only partial values were received. There is a url,url in the data that contains the special symbol &, and the received value is to this symbol, the following is not. For example, data in the source code is the value I want.

Reply content:

In the display page, using AJAX to post the value of the value data is in JSON format. Received in the controller, but only partial values were received. There is a url,url in the data that contains the special symbol &, and the received value is to this symbol, the following is not. For example, data in the source code is the value I want.

Depends on the type you pass to data:

    • In the case of an object, jquery automatically adds the encodeuricomponent,& symbol when the AJAX request is sent, is encoded as%26, and is not automatically truncated by formdata
    • If data is a string, jquery automatically places the string in Formdata and sends it to the server intact, because Formdata is truncated by &, so the string is automatically truncated AT & without encodeURIComponent.

Before submitting, URL encode you pass this URL

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

$(".a_post").on("click",function(event){    event.preventDefault();//使a自带的方法失效    $.ajax({           type: "POST",           url: "url地址",           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){              //请求正确之后的操作           },           error: function(result){              //请求失败之后的操作           }    });});

View the request parameters as follows:

With this json.stringify ({}) encapsulated, is the Ajax transmission of the time of the problem, with the data: ' Data= ' +data, so that the value is a problem, but with data:{data:data}, you can, is why?

  • 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.