I’ve been trying to send a POST request to a web service (written in .NET) using jQuery Mobile . I am using jQuery Mobile along with PhoneGap for iOS and writing the code in XCode.
This is the code I am running –
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);
}
});
This is giving me the following error –
{"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"}
Please help.
The result of your json
would be this:
And that was wrong, it should be:
Equivalent json:
Or since your are passing a single object it must be:
Equivalent json:
Remember that you don’t have to put
""quotes between}curly braces.