2013-02-06 207 views
-1

我想在iOS上使用jQuery Mobile实现身份验证API。我正在发送使用jQuery.ajax()的发布请求,但代码失败。jQuery发布请求失败

代码如下,我正在使用jQuery Mobile框架和iOS的Phonegap。

这段代码有什么问题吗?
是否有可能返回HTTP错误代码(例如500,404等)?

function authenticate() { 
    $.ajax({ 
     url: "https://someurl.com/authenticateUser", 
     type: "POST", 
     dataType: "json", 
     contentType: "application/json; charset=utf-8", 
     data: JSON.stringify("{\"jsonText\": \"{ \"username\" : \"Test\", \"password\" : \"testing123\" } \"} "), 
     success: function (result) { 
      alert(result); 
     }, 
     error: function (result) { 
      console.log(result); 
     } 
    }); 
} 

当我经过HTML测试代码 -

<input id="clickMe" type="button" value="Authenticate Me" onclick="authenticate();" /> 

我得到一个警报视图说:

index.html 
[object Object] 

编辑:读了一些意见后,我现在用的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"} 
+1

请勿使用'alert'进行调试,请使用'console.log'。 – undefined

+1

使用检查员并查看网络流量,这会让您知道发生了什么 – cgatian

+0

感谢您的建议。现在我看到了上面发布的更详细的错误消息 –

回答

0

看着你已经很难说出正在发生什么的退回警报。但是,如果你想知道什么错误代码正在返回,那么你可以尝试像this。此外,您正在使用的POST请求依赖于相同的域?