2

请查看下面的代码并帮助我找出我在 Web 服务代码中做错了什么。我想设置一个可以使用 JSONP 使用的 asp.net Web 服务。我在客户端使用 Jquery 来访问该站点。即使设置了正确的属性,我的 Web 服务仍然会发出 xml,因此 aynch 调用失败。

网络服务

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
 [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

    public WebService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat= ResponseFormat.Json, XmlSerializeString=false, UseHttpGet=true)]
    public string HelloWorld(int id, string __callback) {
        return  __callback + "({message: 'Hello World'})";
    }

}

网络服务响应:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">test({message: 'Hello World'})</string>

网络配置:

<webServices>
    <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
    </protocols>
</webServices>
<httpHandlers>
    <remove verb="*" path="*.asmx"/>
    <add verb="*" path="*.asmx" validate="false" 
        type="System.Web.Script.Services.ScriptHandlerFactory, 
            System.Web.Extensions, Version=3.5.0.0, 
            Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add verb="*" path="*_AppService.axd" validate="false" 
        type="System.Web.Script.Services.ScriptHandlerFactory, 
            System.Web.Extensions, Version=3.5.0.0, 
            Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add verb="GET,HEAD" path="ScriptResource.axd" 
        type="System.Web.Handlers.ScriptResourceHandler, 
            System.Web.Extensions, Version=3.5.0.0, 
            Culture=neutral, PublicKeyToken=31BF3856AD364E35" 
        validate="false"/>
</httpHandlers>
<httpModules>
    <add name="ScriptModule" 
        type="System.Web.Handlers.ScriptModule, System.Web.Extensions, 
            Version=3.5.0.0, Culture=neutral, 
            PublicKeyToken=31BF3856AD364E35"/>
</httpModules>

Javascript

$.ajax({
    type: "GET",
    contentType: "application/json; charset=utf-8",
    url: "http://localhost:54404/jsonp/webservice.asmx/HelloWorld?id=2&__callback=?", //?jsonp=MatchSvcCallback
    dataType: "jsonp",
    data: {},
    //jsonp : "MatchSvcCallback",
    success: function(msg) {
    alert("Inside success callback function"); // not being called

    },
    error: function(xhr, msg){
        var response = JSON.parse(xhr.responseText);

    }

});

js 代码与处理程序一起使用,但由于 xml 响应而导致 web 服务失败。

4

1 回答 1

5

我认为这里的问题是因为 jQueryContent-Type在使用$.ajax(). 仅当请求类型为“POST”时才会发送。这似乎适用于dataType: "jsonp"dataType: "json"

我尝试了您的示例并观看了Fiddler中的请求/响应交换,果然我没有看到Content-Type: application/xml; charset=UTF-8在 HTTP GET 请求的标头中发送。如果使用 HTTP POST,则确实会发送标头。我猜这个标头的存在是 ASP.NET Web 服务管道决定是返回 JSON 还是 XML 格式的响应的一个线索。

看来您不是唯一遇到此问题的人,请参阅优雅代码网站上的以下文章:

从 JQuery 调用远程 ASP.NET Web 服务

解决方案似乎是以下之一:

  • 如上文所述,使用 aHttpModule将标头注入请求流中。Content-Type: application/xml; charset=UTF-8

  • HttpHandler正如您在使用 ASP.NET Web 服务之前所解释的那样,将a用于端点。

  • 在以下文章中尝试 Rick Strahl 的基于页面的方法(HttpHandler无论如何都有效):JSONP for cross-site Callbacks

于 2009-03-11T19:19:16.507 回答