【问题标题】:Cross domain WebService Call not returning expected result跨域 WebService 调用未返回预期结果
【发布时间】:2014-05-13 10:59:21
【问题描述】:

我在 let 中托管了一个网络服务

http://example1.com/webservice.asmx

并想从

http://example2.com

我在 example2.com 中有 jQuery 代码

GetData: function () {
    $.ajax({
        crossDomain: true,
        type: "POST",
        url: "http://example1.com/webservice.asmx/GetData",
        dataType: "jsonp",
        contentType: "application/json; charset=utf-8",
        data: { Date: '' },
        success: function (data, textStatus, jqXHR) {
            debugger;                
        },
        error: function (data, textStatus, jqXHR) {
            alert("data");
        }
    });
  }

它像

一样点击 url
http://example1.com/webservice.asmx/GetData?callback=jQuery19106349606812515739_1396429620115&Date=&_=1396429620116

它使用 GET 方法(来自 firebug)点击了该 url。实际上我找不到问题在哪里。 它以 XML 格式响应数据。 并且还以 XML 格式响应数据,但未获得成功事件。 但如果我将相同的代码放在同一个域上,它就可以正常工作。

【问题讨论】:

  • 您的网络服务是否使用回调包装了响应?例如jQuery19106349606812515739_1396429620115({'iam':'the json response'});?
  • 不,我不知道从哪里将 jQuery19106349606812515739_1396429620115 添加到请求的服务 url。
  • 你知道dataType: "jsonp"是什么意思吗?

标签: jquery ajax web-services


【解决方案1】:

对于 JSONP,您的响应必须包含在 Javascript 函数中。如果您设置dataType: "jsonp",Jquery 会自动将回调参数添加到 GET-URL。这个回调参数是一个随机的js函数名,你的ajax请求需要它来恢复数据跨源。

要使其正常工作,您需要按以下方式更改您的 Web 服务:

  1. 更改为 JSON
  2. 使用回调参数包装响应。例如在 VB.NET 中:

    Dim returnVal=Request.Param("Callback") & "(" & jsonreturn & ");"
    Response.Write(returnVal) 
    

请参阅post 了解详细信息,以使其与您的 asmx 网络服务一起使用。或者,您可以将您的 Web 服务更改为 CORS

【讨论】:

    【解决方案2】:

    此修复适用于来自 JQuery 的纯 json 调用。 从 system.web 中的 web.config 启用 HttpGet 和 Post

    <webServices>
      <protocols>
        <add name="HttpSoap"/>
        <add name="HttpPost"/>
        <add name="HttpGet"/>
        <add name="HttpPostLocalhost"/>
      </protocols>
    </webServices>
    

    然后,在您的 Global.asax 文件中创建一个方法以启用跨域通信 (vb.net) '==========================EnableCrossDmainAjaxCall=================

    Private Sub EnableCrossDmainAjaxCall()
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*")
    
        If HttpContext.Current.Request.HttpMethod = "OPTIONS" Then 
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS")
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept")
            HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000")
    
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "origin, x-requested-with, content-type") 
            HttpContext.Current.Response.[End]()
        End If
    
    End Sub
    
    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache)
        HttpContext.Current.Response.Cache.SetNoStore()
        EnableCrossDmainAjaxCall() 
    End Sub
    '==========================
    

    【讨论】:

      猜你喜欢
      • 2011-08-09
      • 2010-09-22
      • 2021-05-17
      • 2018-02-19
      • 2018-03-23
      • 2020-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多