Parameter with '&' breaking $.ajax request

核能气质少年 提交于 2019-12-18 07:00:26

问题


I'm developing a Web App that uses JavaScript + JQuery on the client side and PHP on the server side.

One of the strings I want to pass as parameter for an AJAX Request has a '&' in its content.

For this reason, the string of the request is broken. The browser "thinks" that this parameters is over because there is a '&' on the string.

var hasChar = "This is a string that has a & in the content.";
var doesntHave = "This one does not contain.";
var dataString = "first=" + hasChar + "&second=" + doesntHave;

$.ajax({
    type : "POST",
    url : "myurl.php",
    data : dataString,
    cache : false,
    success : function(html) {
    }
});

The server receives the first parameter as "This is a string that has a "

My Question:

How to I encode the string on the client side and how should I decode it on the PHP server.


回答1:


Why not to do following:

    $.ajax({
        type : "POST",
        url : "myurl.php",
        data : {
          'first': hasChar,
          'second': doesntHave
        },
        cache : false,
        success : function(html) {
        }
    });

In this case jQuery will make sure that string is properly encoded.

As an alternative you can use encodeURIComponent() JS's built in function for properly encoding strings:

var dataString = "first=" + encodeURIComponent(hasChar) + "&second=" + encodeURIComponent(doesntHave);



回答2:


Let jQuery handle the encoding of hasChar (and your other params) for you:

var hasChar = "This is a string that has a & in the content.";
var doesntHave = "This one does not contain.";

$.ajax({
    type : "POST",
    url : "myurl.php",
    data : { first: hasChar, second: doesntHave },
    cache : false,
    success : function(html) {
    }
});



回答3:


You can use .param;

dataString = $.param({first: asChar, second: doesntHave});



回答4:


or if you want to skip the $.param part as metioned by @alex-k

data : {'first': hasChar, 'second': doesntHave},



回答5:


You can just set it as an object:

var hasChar = "This is a string that has a & in the content.";
var doesntHave = "This one does not contain.";

$.ajax({
    type : "POST",
    url : "myurl.php",
    data : {first: hasChar, second: doesntHave},
    cache : false,
    success : function(html) {
    }
});



回答6:


You can also use encodeURI:

var encodedData = encodeURI(dataString);
$.ajax({
    type : "POST",
    url : "myurl.php",
    data : encodedData,
    cache : false,
    success : function(html) {
    }
});

Link




回答7:


If you want server-side de/encoding use urlencode() and urldecode().




回答8:


Try this

var dataString = {};
dataString["hasChar"] = "This is a string that has a & in the content.";
dataString["doesntHave"] = "This one does not contain.";

$.ajax({
    type : "POST",
    url : "myurl.php",
    data : dataString,
    cache : false,
    success : function(html) {
    }
});



回答9:


Your parameters need to be simply URL-encoded. On the PHP side, you won't need to decode them, everything will work as normal.

There is a Javascript function called encodeURIComponent() that will correctly URL-encode your string. So on the basic level, you can do this:

var dataString = "first=" + encodeURIComponent(hasChar) + "&second=" + encodeURIComponent(doesntHave);

If you use jQuery, it will automatically handle this for you, if in your $.ajax() call you pass it an object instead of a query string:

The data option can contain either a query string of the form key1=value1&key2=value2, or a map of the form {key1: 'value1', key2: 'value2'}. If the latter form is used, the data is converted into a query string using jQuery.param() before it is sent.

So you only have to do this in your code:

var dataString = { first: hasChar, second: doesntHave);


来源:https://stackoverflow.com/questions/8184438/parameter-with-breaking-ajax-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!