我有这个ajax电话:
$.ajax( {
type: "POST",
url: "../someService",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: '{ prefixText:"'+ some"text +'", count:10 }',
success: function( data ) {
response(data.d);
},
error: function (error) {
alert("error")
}
});
执行代码时出现错误,因为我发送给webService的字符串包含双引号。
如何更改我不会收到错误的字符串?
答案 0 :(得分:2)
You're sending JSON, so you need to be sure to send valid JSON. Your string is not valid JSON for at least a couple of reasons: You don't have your property keys in quotes, and you have a syntax error because of quoting.
Don't hand-create JSON. Instead, create the object and use a serializer to create the JSON for it:
$.ajax( {
type: "POST",
url: "../someService",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify({ prefixText: "some" + text, count: 10 }), // ***
success: function( data ) {
response(data.d);
},
error: function (error) {
alert("error")
}
});
答案 1 :(得分:0)
当您使用JSON作为数据类型时,您可以直接使用该对象,如
$.ajax( {
type: "POST",
url: "../someService",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: { prefixText: "some" + text, count: 10 },
success: function( data ) {
response(data.d);
},
error: function (error) {
alert("error")
}
});