How do I deal with special characters in the parameters passed in the AJAX request?

Source: Internet
Author: User

In the jquery Ajax development encountered the following problem, in the implementation of the AJAX request, you need to pass a "50%" parameter, and this parameter contains a special character%. This will cause problems, and we know that when the server sends the request, the parameters in the URL are converted to "%2c%2f%3f%3a%40%26%3d%2b%24%23", so we get an error when we execute an AJAX request that contains such a special character. Because the parameters we pass may change, or because the parameters themselves contain special characters, there is no way for the system to compile it.

Such an exception might be thrown:

Warning: Parameters:character decoding failed. Parameter skipped.

The solution:

1, change the form of transfer parameters:

For example:

var testUrl ="param1="+cond1+"&params2="+cond2 ;$.ajax({    type:"POST",    url:"testUrl",    data:allParams,    success:function(data){    }});

In the above code,

If Cond2 = "50%", then there is a% special character in this parameter, so the timing of the execution can be problematic. How to modify it?

We talked about changing the form of passing parameters.

Modify the data section to pass in JSON format so that you can avoid error parameter problems caused by special characters:

data:{' param1 ': cond1, ' param2 ': Cond2}

2, or in accordance with the method of transmission in Example 1 to pass the parameters, but to achieve our goal, we have to pass the parameters to encode.

For example, if you are still example 1, we will modify Testurl to the following notation, using the encodeURIComponent (urlstring) function to encode the parameters passed in the URL:

var testUrl ="param1="+cond1+"&params2="+encodeURIComponent(cond2);

How do I deal with special characters in the parameters passed in the AJAX request?

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.