我發現很多「解決方案」來解決這個看似普遍的問題,但是,唉,當然沒有一個可以爲我工作。我有一些試圖發佈到ASP.NET 3.5 Web服務的jQuery代碼。 Web服務應該返回一些JSON。使用下面的代碼,我得到了501內部服務器錯誤,但根據在線發現的解決方案,這是它應該是這樣的! jquery代碼中的錯誤函數顯示一個parsererror。如果我更改數據參數如何傳遞給服務("userID=3456"
),並刪除contentType屬性並將dataType更改爲「text」,則不會出現錯誤,但會返回XML字符串中的JSON。使用jQuery無法獲得從ASP.NET web servive返回的JSON對象
這是我的jQuery代碼:
$.ajax({
type: "POST",
url: "mywebservice.asmx/Initialize",
data: "{'userID': '3456'}",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(result) {
alert("success");
},
error: function(request, status, errorThrown) {
alert(status);
alert(errorThrown);
}
});
這是我的ASP.NET代碼:
<WebMethod()> _
Public Function Initialize(ByVal userID As Integer) As String
Dim scormInstance As New CMI(userID)
Return scormInstance.ToJsonString
End Function
這裏是通過瀏覽器中的HTTP請求和響應(火狐3.5.1)
http://localhost/cognition/webservices/cognitionapi.asmx/Initialize
POST /cognition/webservices/cognitionapi.asmx/Initialize HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1 (.NET CLR 3.5.30729)
Accept: application/json, text/javascript, */*
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Content-Type: application/json; charset=utf-8
X-Requested-With: XMLHttpRequest
Referer: http://localhost/cognition/scorm-test2.html
Content-Length: 20
Cookie: RadEditorGlobalSerializeCookie=[_ctl0__ctl0__ctl0_Content_mainContent_mainContent_txtSpecialInstructionsModules]-[]#[_ctl0__ctl0__ctl0_Content_mainContent_mainContent_txtSpecialInstructionsToolbars]-[]#
Pragma: no-cache
Cache-Control: no-cache
{"userID":"1234"}
HTTP/1.x 500 Internal Server Error
Cache-Control: private
Content-Length: 4956
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/7.0
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
Date: Mon, 27 Jul 2009 21:24:41 GMT
任何想法,究竟我在做什麼錯???? !!! ???
謝謝!
我試過了,沒有這樣的運氣。 – 2009-07-27 21:37:12