-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"}
請勿使用'alert'進行調試,請使用'console.log'。 – undefined
使用檢查員並查看網絡流量,這會讓您知道發生了什麼 – cgatian
感謝您的建議。現在我看到了上面發佈的更詳細的錯誤消息 –