如果用户在textarea中输入引号,我无法通过我的ajax调用发送数据。有没有办法发送带引号的文字?我不想摆脱报价。我想这通常不是一个大问题,我在网上发现这种情况很少。
var description = $('#Description').val();
var title = $('#Title').val();
var parameters = '{content:' + $('#ContentCheck').is(':checked') +
',title: "' + title + '",desciption:"' + description + '"}';
答案 0 :(得分:4)
结帐{j}内置的JSON.stringify(object)
。
jist是,你创建一个javascript对象,并调用stringify来创建一个JSON字符串。根据您上面给出的信息,您可能会这样做:
var jsonText = JSON.stringify({
'content': $('$('#ContentCheck').is(':checked'),
'title': title,
'description': description
});
这里我们使用花括号定义javascript哈希,然后将其传递给stringify。
答案 1 :(得分:2)
同样的问题在我身上。我试过这个并且完全正常工作
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);
并在插入时使用urldecode($ _ REQUEST ['details'])`。
答案 2 :(得分:1)
您可以将参数转换为地图 - 基本上是关联数组 - 而不是字符串。这样,您可以在后端.NET中进行字符串操作,而不需要在前端js中进行任何更改; e.g。
var url = 'http://path/to/your/backend/script';
var parameters = {
contentCheck: $('#ContentCheck').is(':checked'),
title: title,
description: description
};
$.post(url, parameters);
答案 3 :(得分:0)
您可以使用js编码引号并在服务器端解码以获取原始字符串
// JS
function totalEncode(s){
str= s.replace(/\&/g,'&');
str= str.replace(/</g,'<');
str= str.replace(/>/g,'>');
str= str.replace(/\"/g,'"');
str= str.replace(/\'/g,''');
return(str);
}