繁体   English   中英

使用jQuery的CORS请求-$ .ajax()

[英]CORS request using jQuery - $.ajax()

我正在开发一个Web应用程序,其中数据来自不同的域。 我的意思是在我的应用程序中,几乎90%的请求都是跨域请求。

在IIS上部署此应用程序时,无法获取数据。

服务器部署在http://some.ip.add/crm服务客户端部署在http://diffent.ip.add/saascrm

我正在使用jQuery 2.0以异步方式使用$ .ajax()获取数据。

注意: 数据以xml格式传入

还向web.config文件添加了一些内容。

<system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
      </customHeaders>
    </httpProtocol>
</system.webServer>

这是我的片段。

$.support.cors = true;
      $.ajax({
                        type: "GET",
                        url: 'http://some.ip.add/crmservice/crmservice.asmx/HandShake', 
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        crossDomain: true,
                        beforeSend: function (request) {
                        //    debugger;
                            request.setRequestHeader("Access-Control-Allow-Origin", '*');
                        },
                        error: function (xhr, status, error) {
                            try {
                              //  debugger;
                                // debugger;
                              //Here i am getting error : Access denied in IE 9.0 and and just "error" in firefox. 
                                var msg = JSON.parse(xhr.responseText);
                                alert(msg.Message);
                            }
                            catch (e) {
                                // debugger;
                                alert(xhr.statusText);
                            }
                            return true;

                        },
                        success: function (data) {
                            debugger;
                            xmlDoc1 = $.parseXML(data.d);
                            $xml1 = $(xmlDoc1);
                            if ($xml1.find('Result').text() == '0') {
                                $(this).MessageBox('success', $xml1.find('Message').text());
                                $("#uxDBName").prop("disabled", false);
                                $("#uxSUPassword").prop("disabled", false);
                                $("#uxServiceURL").prop("disabled", true);
                                GetListOfB1Databases(url);
                            }
                        }
                    });

我的服务器代码是:

Global.asax

protected void Application_BeginRequest(object sender, EventArgs e)
    {
        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        HttpContext.Current.Response.Cache.SetNoStore();
        EnableCrossDmainAjaxCall();  
    }
    private void EnableCrossDmainAjaxCall()
    {
        HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Origin", "*");

        if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
        {
            HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Methods", "GET, POST");
            HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Headers","Content-Type, Accept");
            HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Origin", "*");
            HttpContext.Current.Response.AppendHeader("Access-Control-Max-Age", "1728000");
            HttpContext.Current.Response.End();
        }
    }

     //Web method
     [ScriptMethod(ResponseFormat = ResponseFormat.Json), WebMethod(EnableSession = true)]
    public string HandShake()
    {
        return General.Response("0", "Tenant is in reachable. Please specify SAP Business One Company database\r\nand 'manager' Password", "");
    }

我发现了一些解决方案, 也是,我发现CORS不被IE 8与9,IE 8 * 9不创建XMLHttpRequest对象的实例支持。 它创建XDomainRequest,因此需要检查用户代理。 我在这里找到了替代解决方案

现在我的问题是我几乎在跨域调用中到处都有$ .ajax()方法。 我不想在我的框架中进行重大更改。

使用$ .ajax()有什么解决方案吗?

请帮助我,因为一个星期以来,我严重卡住了。

提前致谢。

感谢您的合作和提供的帮助。 我找到了解决方案。

  var url = $("#uxServiceURL").val(); $.ajax({ crossOrigin: true, url: url + '/HandShake', error: function (xhr, status, error) { try { alert('Error'); } catch (e) { alert(xhr.statusText); } return true; }, success: function (data) { var d1 = data.replace(/\\&lt;/g, '<').replace(/\\&gt;/g, '>') xmlDoc1 = $.parseXML(d1); $xml1 = $(xmlDoc1); if ($xml1.find('Result').text() == '0') { $(this).MessageBox('success', $xml1.find('Message').text()); } } }); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM