jQuery.ajax中的双引号和\
我一直在尝试使用jQuery Mobile向web服务(用.NET编写)发送POST请求。我正在使用jQuery Mobile和PhoneGap for iOS,并在XCode中编写代码 这是我正在运行的代码-jQuery.ajax中的双引号和\,jquery,cordova,jquery-mobile,Jquery,Cordova,Jquery Mobile,我一直在尝试使用jQuery Mobile向web服务(用.NET编写)发送POST请求。我正在使用jQuery Mobile和PhoneGap for iOS,并在XCode中编写代码 这是我正在运行的代码- var param = "{\"jsonText\": \"{ \"username\" : \"Test\", \"password\" : \"testing123\" } \"} "; console.log(param)
var param = "{\"jsonText\": \"{ \"username\" : \"Test\", \"password\" : \"testing123\" } \"} ";
console.log(param)
$.ajax({
url: "https://example.asmx/authenticateUser",
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
data:JSON.stringify(param),
success: function(result){
console.log(result);
},
error: function(result){
console.log(result);
}
});
这给了我以下的错误-
{"readyState":4,"responseText":"{\"Message\":\"Cannot convert object of type \\u0027System.String\\u0027 to type
\\u0027System.Collections.Generic.IDictionary`2[System.String,System.Object]\\u0027\",\"StackTrace\":\" at
System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\\r\\n at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\\r\\n at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToType(Object o, Type type, JavaScriptSerializer serializer)\\r\\n at
System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\\r\\n at
System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\\r\\n
at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\\r\\n
at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\\r\\n
at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData
methodData)\",\"ExceptionType\":\"System.InvalidOperationException\"}","status":500,"statusText":"Internal Server Error"}
请帮助。您想使用
JSON.parse
而不是JSON.stringify
。前者从JSON字符串(您拥有的)获取对象。后者将JavaScript结构转换为这样的字符串
此外,您的JSON是无效的。{
周围不应有引号:
JSON.parse("{\"jsonText\": { \"username\" : \"Test\", \"password\" : \"testing123\" } } ")
json的结果
"{\"jsonText\": \"{ \"username\" : \"Test\", \"password\" : \"testing123\" } \"} "
是这样的:
{
"jsonText": "{
"username": "Test",
"password": "testing123"
}"
}
这是错误的,应该是:
{
"jsonText":{
"username": "Test",
"password": "testing123"
}
}
等效json:
"{\"jsonText\":{\"username\":\"Test\", \"password\":\"testing123\"}}"
{\"username\":\"Test\",\"password\":\"testing123\"}
或者,由于您要传递单个对象,因此必须:
{
"username": "Test",
"password": "testing123"
}
等效json:
"{\"jsonText\":{\"username\":\"Test\", \"password\":\"testing123\"}}"
{\"username\":\"Test\",\"password\":\"testing123\"}
请记住,您不必在
}
大括号之间加引号。您可以使用单引号在JavaScript中包装字符串。为什么要手动创建JSON字符串作为开始?只需创建一个对象文本,并让JSON
方法对其进行操作