开发者

Sending parameters in Jquery Ajax call with quotes, double or single

I am unable to send data through my ajax call if the user put quotes in the textarea. Is there a way to send text with quotes? I dont want to get rid of the quotes. I guess this is usually not a big problem, i found very little online about this situation.

var description = $('#Description').val();
var title = $开发者_如何学Python('#Title').val();
var parameters = '{content:' + $('#ContentCheck').is(':checked') +
                        ',title: "' + title + '",desciption:"' + description + '"}';


Checkout JSON.stringify(object) which is built into javascript.

The jist is, you create a javascript object, and call stringify to create a JSON string. With your information given above, you might do:

var jsonText = JSON.stringify({
    'content': $('$('#ContentCheck').is(':checked'),
    'title':   title,
    'description': description
});

Here we define a javascript hash using the curly braces, and then pass it to stringify.


same problem was with me.I tried this and completely working

 xmlhttp.open("POST","test_test.php",true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");

    xmlhttp.send("name="+encodeURIComponent(name)+"&details="+encodeURIComponent(details)+"&r_price="+r_price+"&o_price="+o_price+"&id="+id);

and use urldecode($_REQUEST['details'])` when inserting.


You can convert parameters into a map -- basically an associative array -- instead of a string. That way, you can do string manipulation in the backend .NET without any changes in your frontend js; e.g.

var url = 'http://path/to/your/backend/script';
var parameters = {
    contentCheck: $('#ContentCheck').is(':checked'),
    title: title,
    description: description
};
$.post(url, parameters);


You can encode quotes using js and decode it server side to get the original string

//js

function totalEncode(s){
str= s.replace(/\&/g,'&');
str= str.replace(/</g,'&lt;');
str= str.replace(/>/g,'&gt;');
str= str.replace(/\"/g,'&quot;');
str= str.replace(/\'/g,'&#039;');
return(str);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜