1

jQuery Mobile を使用して、POST リクエストを Web サービス (.NET で記述) に送信しようとしています。iOS用のPhoneGapと一緒にjQuery Mobileを使用し、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.parseないJSON.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 に答える