【发布时间】:2013-05-10 14:19:47
【问题描述】:
我的问题与here 描述的问题非常相似。
但是,在我的情况下,我在 firebug 中收到 500 Internal Server Error:
请求格式无法识别,因为 URL 意外以“/HelloWorld”结尾。
我的 asp.net 4.0 Web 服务调用是跨域的,并且根据十几个其他网站的建议,我相信我已经配置了所有内容以使这种情况正确发生,但显然我没有。我究竟做错了什么?
我使用 JSONP 而不是 JSON。如果一切都在同一台服务器上,则 Web 服务将按预期工作。我的问题是调用 Web 服务的 html 页面的托管服务提供商不允许服务器端代码,否则我只需将所有内容放在一个地方并完成它!
下面是我的代码/标记:
web.config:
<?xml version="1.0"?>
<configuration>
<system.webServer>
<httpErrors errorMode="Detailed"></httpErrors>
<asp scriptErrorSentToBrowser="true"></asp>
<modules>
<add name="ContentTypeHttpModule"
type="ContentTypeHttpModule.ContentTypeHttpModule, ContentTypeHttpModule" />
</modules>
</system.webServer>
<system.web>
<customErrors mode="Off"></customErrors>
<compilation debug="true" targetFramework="4.0"/>
<trust level="Full"/>
<pages clientIDMode="Static"/>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
<!--<httpModules>
<add name="ContentTypeHttpModule"
type="ContentTypeHttpModule.ContentTypeHttpModule, ContentTypeHttpModule" />
</httpModules>-->
</system.web>
</configuration>
html 文件 javascript:
function test() {
$.ajax({
url: 'http://designonlinelettering.com/RenderImage.asmx/HelloWorld',
data: {},
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
success: function (result) {
var data = result.d;
alert(data);
},
error: function (e) {
alert('error: ' + e.d);
}
});
}
网络服务代码:
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string HelloWorld()
{
return "Hello World";
}
ContentTypeHttpModule 代码取自这个信息量很大的blog,我从中得到了大部分的指导。
感谢您的帮助...
【问题讨论】:
-
jsonp 请求不需要 $.ajax 的 contentType 和错误选项,仅供参考
-
您得到的错误是服务器端的,与 jQuery 无关。直接导航到
http://designonlinelettering.com/RenderImage.asmx/HelloWorld,你就会看到。
标签: javascript jquery asp.net web-services jsonp