admin管理员组

文章数量:1323560

I have an ajax call as

$.ajax({
    url: 'url&para1=' + JSON.stringify(obj),
    type: 'POST',
    async: true,
    contentType: 'application/json',
    data: {},
    success: function(data){
    },
    error: function(error){

    }
});

In the url i get %22 for every "

How can that be escaped?

I have an ajax call as

$.ajax({
    url: 'url&para1=' + JSON.stringify(obj),
    type: 'POST',
    async: true,
    contentType: 'application/json',
    data: {},
    success: function(data){
    },
    error: function(error){

    }
});

In the url i get %22 for every "

How can that be escaped?

Share Improve this question asked Jun 3, 2014 at 19:13 user544079user544079 16.6k42 gold badges120 silver badges172 bronze badges 1
  • 2 You are sending your json data in URL query string and sending data:{} in a type: "POST", oh boy, I think there are far more optimization options than just escaping the ". Try a different approach, use data for posting POST data. That would be the best solution. – brainless coder Commented Jun 3, 2014 at 19:18
Add a ment  | 

3 Answers 3

Reset to default 4

Maybe this will help

url: 'url&para1=' + encodeURIComponent(JSON.stringify(obj)),

%22 is the "escaped" value for a double quote. It's the accepted way to send that data in a URI. Where exactly are you running into a problem?

The JSON.stringify converts the object into a string. However, a string with quotes must be converted to a URL encoded string (see superrafal's answer).

However, if this is a POST request, why are you sending parameters in the URL as a query string (?key=value) like a GET request? The data parameter is set to a blank object. If you desire to send those values in the URL as a GET, change the type to GET, remove the "data" parameter, and use $.param(obj) to convert that object to a query string. If you desire to send those values as a POST, use the following:

$.ajax({
    url: "url_to_file_to_accept_POST_request",
    type: "POST",
    data: obj,
    success: function(data) {

    },
    error: function(error) {

    }
});

本文标签: javascriptescape double quotes in url of ajax callStack Overflow