gk.*_*gk. 2 javascript asp.net jquery jsonp web-services
请查看下面的代码并帮助我弄清楚我的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>
Web.Config中:
<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响应而导致webservice失败.
我认为这里的问题是因为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格式的响应.
您似乎并不是唯一遇到此问题的人,请参阅关于优雅代码网站的以下文章:
解决方案似乎是以下之一:
使用a HttpModule将Content-Type: application/xml; charset=UTF-8标头注入请求流,如上文所述.
HttpHandler如您在使用ASP.NET Web服务之前所解释的那样,使用a 作为端点.
在下面的文章中尝试Rick Strahl的基于页面的方法(实际上HttpHandler无论如何都是这样):用于跨站点回调的JSONP.
| 归档时间: | 
 | 
| 查看次数: | 7415 次 | 
| 最近记录: |