代码之家  ›  专栏  ›  技术社区  ›  gk.

使用JQuery-JSONP访问ASP.net Web服务时出错

  •  2
  • gk.  · 技术社区  · 17 年前

    请查看下面的代码,并帮助我找出我在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'})";
        }
    
    }
    

    Web服务响应:

    <?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>
    

    $.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服务失败。

    1 回复  |  直到 17 年前
        1
  •  5
  •   erlando    12 年前

    我认为这里的问题是因为jQuery没有设置 Content-Type 使用HTTP GET时HTTP请求中的标头 $.ajax() 。仅当请求类型为“POST”时才会发送。两者似乎都是如此 dataType: "jsonp" 和 dataType: "json" .

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

    看来你并不是唯一一个有这个问题的人,请参阅Elegant Code网站上的以下文章:

    Calling Remote ASP.NET Web Services from JQuery

    • 使用 HttpModule 注射 内容类型:应用程序/xml;字符集=UTF-8 如上文所述,将标头插入请求流。

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

    • 试试Rick Strahl的基于页面的方法 在以下文章中( 实际上是a 处理中心 无论如何): JSONP for cross-site Callbacks .