出错访问使用JQuery ASP.net Web服务 - JSONP [英] Error while accessing ASP.net webservice using JQuery - JSONP

查看:109
本文介绍了出错访问使用JQuery ASP.net Web服务 - JSONP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请看看下面的code和帮助我找出我做错了我的web服务code。我想设置一个可以使用JSONP消耗一个asp.net web服务。我使用Jquery在客户端访问该网站。即使正确设置后的属性我的web服务仍然由于其aynch调用失败发出的XML。

Please look at the code below and help me to figure out what am I doing wrong in my web service code. I want to set up an asp.net web service that can be consumed using an JSONP. I am using Jquery at client side to access the site. Even after setting up proper attributes my web service still emits xml due to which the aynch call fails.

Web服务

[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服务响应:

Web Service Response:

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

Web.Config中:

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

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的code工作与处理程序,但无法为WebService由于XML响应。

The js code works with an handler, but fails for the webservice due to xml response.

推荐答案

我觉得这里的问题是因为jQuery不设置内容类型头中的HTTP使用时,请求HTTP GET的 $。阿贾克斯()。它仅被发送如果请求类型是POST。这似乎是两个的dataType的情况:​​JSONP数据类型:JSON

I think the problem here is because jQuery doesn't set the Content-Type header in the HTTP request for HTTP GET's when using $.ajax(). It only gets sent if the request type is 'POST'. This seems to be the case for both dataType: "jsonp" and dataType: "json".

我想你的榜样,并观看了提琴手果然我不请求/响应交换吨看到的Content-Type:application / xml进行;字符集= UTF-8 的头信息中发送的HTTP GET请求。头不会得到,如果使用HTTP POST发送。我猜这头的presence是ASP.NET Web服务管道的提示,以决定是否返回一个JSON或XML格式的响应。

I tried your example and watched the request/response exchange in Fiddler and sure enough I don't see Content-Type: application/xml; charset=UTF-8 being sent in the headers for HTTP GET requests. The header does get sent if using HTTP POST. I'm guessing the presence of this header is a cue for the ASP.NET web service plumbing to decide whether to return either a JSON or XML formatted response.

看来你不是唯一一个这样的问题,请参阅上优雅code网站上的以下文章:

It seems you're not the only one with this problem, see the following article on the on Elegant Code website:

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

解决的办法似乎是下列之一:

The solution appears to be one of the following:


  • 使用的HttpModule 注入的Content-Type:application / xml进行;字符集= UTF-8 头到如上文中描述的请求流。

  • Use a HttpModule to inject the Content-Type: application/xml; charset=UTF-8 header into the request stream as described in the article above.

使用的HttpHandler 端点为你解释你之前使用ASP.NET Web服务在做什么。

Use a HttpHandler for the endpoint as you explained you were doing prior to using an ASP.NET web service.

尝试里克·斯特劳的基于页面的方法
下面的文章中(其中
实际上是一个的HttpHandler 反正):的 JSONP跨站点回调

Try Rick Strahl's page based approach in the following article (which is in effect a HttpHandler anyway): JSONP for cross-site Callbacks.

这篇关于出错访问使用JQuery ASP.net Web服务 - JSONP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆