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

CORS request using jQuery - $.ajax()

本文关键字:ajax 请求 jQuery CORS 使用      更新时间:2023-09-26

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

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

服务器部署在 http://some.ip.add/crmservice客户端部署在 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);
                            }
                        }
                    });

我的服务器代码是:

全球

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", "");
    }

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

现在我的问题是我在任何地方都使用 $.ajax() 方法,几乎 90% 的调用是跨域调用。我不想在我的框架中进行此重大更改。

使用 $.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());
                   
                }
            }
        });