I am finding many "solutions" to this seemingly common problem, but alas, none seem to work for me - of course. I have some jQuery code that is trying to post to an ASP.NET 3.5 web service. The web service is supposed to return some json. I am getting 501 Internal Server errors with the code below, but according to the solutions found online, this is how it is supposed to be! The error function in the jquery code displays a parsererror. If I change how the data parameters are passed to the service ("userID=3456"
), and remove the contentType property and change the dataType to "text", then no errors, but it returns the JSON in an XML string.
我正在为这个看似常见的问题找到许多“解决方案”,但唉,似乎没有一个对我有用 - 当然。我有一些jQuery代码试图发布到ASP.NET 3.5 Web服务。 Web服务应该返回一些json。我使用下面的代码得到501内部服务器错误,但根据在线找到的解决方案,这是它应该是这样的! jquery代码中的error函数显示parsererror。如果我更改数据参数传递给服务的方式(“userID = 3456”),并删除contentType属性并将dataType更改为“text”,则没有错误,但它返回XML字符串中的JSON。
Here is my jQuery code:
这是我的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);
}
});
Here is my ASP.NET code:
这是我的ASP.NET代码:
<WebMethod()> _
Public Function Initialize(ByVal userID As Integer) As String
Dim scormInstance As New CMI(userID)
Return scormInstance.ToJsonString
End Function
Here are the HTTP request and response through my browser (Firefox 3.5.1)
以下是通过我的浏览器的HTTP请求和响应(Firefox 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
Any ideas as to what exactly I am doing wrong????!!!!???
关于我究竟做错了什么的任何想法???? !!!! ???
Thanks!
4 个解决方案
#1
After looking at some code i have that's working i found this decoration on the function:
看了一些代码,我有工作,我在功能上找到了这个装饰:
[WebMethod(), ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
also the postdata im passing in is in Json format, ie:
postdata im传入也是Json格式,即:
var postdata = "{'variable':'value'}";
#2
Have you tried using a debugger and seeing if that happens to catch something? I believe some reasons for ajax call failures will show up when you have a debugger attached but you don't get much useful information on the page itself.
您是否尝试过使用调试器并查看是否碰到了什么?我相信当你附加一个调试器时,ajax调用失败的一些原因会出现,但是你没有在页面本身获得太多有用的信息。
The event log may also have something useful in it.
事件日志中也可能包含一些有用的东西。
#3
Does your class have the ScriptService attribute?? In C#:
你的类是否有ScriptService属性?在C#中:
[System.Web.Script.Services.ScriptService]
public class MyWebServiceClass
{
...
}
Otherwise use Firebug to check the error details and provide them here.
否则,请使用Firebug检查错误详细信息并在此处提供。
#4
Your webservice is expecting an int parameter, therefore you need to send a number value. Remove the quotes off of 3456. Check out json.org for type formats.
您的Web服务期望一个int参数,因此您需要发送一个数字值。删除3456之外的引号。查看json.org的类型格式。
#1
After looking at some code i have that's working i found this decoration on the function:
看了一些代码,我有工作,我在功能上找到了这个装饰:
[WebMethod(), ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
also the postdata im passing in is in Json format, ie:
postdata im传入也是Json格式,即:
var postdata = "{'variable':'value'}";
#2
Have you tried using a debugger and seeing if that happens to catch something? I believe some reasons for ajax call failures will show up when you have a debugger attached but you don't get much useful information on the page itself.
您是否尝试过使用调试器并查看是否碰到了什么?我相信当你附加一个调试器时,ajax调用失败的一些原因会出现,但是你没有在页面本身获得太多有用的信息。
The event log may also have something useful in it.
事件日志中也可能包含一些有用的东西。
#3
Does your class have the ScriptService attribute?? In C#:
你的类是否有ScriptService属性?在C#中:
[System.Web.Script.Services.ScriptService]
public class MyWebServiceClass
{
...
}
Otherwise use Firebug to check the error details and provide them here.
否则,请使用Firebug检查错误详细信息并在此处提供。
#4
Your webservice is expecting an int parameter, therefore you need to send a number value. Remove the quotes off of 3456. Check out json.org for type formats.
您的Web服务期望一个int参数,因此您需要发送一个数字值。删除3456之外的引号。查看json.org的类型格式。