1

我一直在尝试使用 jQuery Mobile 向 Web 服务(用 .NET 编写)发送 POST 请求。我正在使用 jQuery Mobile 和 iOS 的 PhoneGap 并在 XCode 中编写代码。

这是我正在运行的代码 -

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"}

请帮忙。

4

2 回答 2

1

你想用JSON.parseJSON.stringify。前者从 JSON 字符串(你所拥有的)中获取一个对象。后者将 JavaScript 结构转换为这样的字符串。

此外,您的 JSON 无效。周围不应有引号{

JSON.parse("{\"jsonText\":  { \"username\" : \"Test\", \"password\" : \"testing123\" } } ")
于 2013-02-06T01:37:48.997 回答
1

你的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"
}

等效的json:

{\"username\":\"Test\",\"password\":\"testing123\"}

请记住,您不必在花括号""之间加上引号。}

于 2013-02-06T02:00:34.850 回答