简体   繁体   English

jquery.ajax Access-Control-Allow-Origin

[英]jquery.ajax Access-Control-Allow-Origin

So here is my call: 所以这是我的电话:

   $.ajax({
       url: url,
       headers: { 'Access-Control-Allow-Origin': '*' },
       crossDomain: true,
       success: function () { alert('it works') },
       error: function() {alert('it doesnt work')},
       datatype: 'jsonp'
   });

My url is legit. 我的网址是合法的。 You will notice that i do not have data set. 您会注意到我没有数据集。 I m not sure if datatype is working properly as its actually xml being returned, but i tried that too. 我不确定数据类型是否正常工作,因为它实际上返回了xml,但我也尝试过。 Its a call to sportsdata's api. 它呼吁sportsdata的api。 On the site, they show you a request header of x-originating-ip so i have tried that where access-control-allow-origin is. 在网站上,他们向您显示x-originating-ip的请求标头,所以我尝试过access-control-allow-origin所在的位置。

All of this still returned the access-control error. 所有这些仍然返回了访问控制错误。 I am not clear on what data is if i set it, so i have omitted it for now. 如果我设置它,我不清楚数据是什么,所以我暂时省略了它。 I have tried a few different things i googled, i understand why i am getting the error. 我用谷歌搜索了几个不同的东西,我理解为什么我得到错误。 I do not know how to fix it. 我不知道如何解决它。 I tried to not have to ask, but if someone could explain or show me the way, that would be greatly appreciated 我试图不必问,但如果有人可以解释或告诉我的方式,那将非常感激

http://encosia.com/using-cors-to-access-asp-net-services-across-domains/ http://encosia.com/using-cors-to-access-asp-net-services-across-domains/

refer the above link for more details on Cross domain resource sharing. 有关跨域资源共享的更多详细信息,请参阅上面的链接。

you can try using JSONP . 你可以尝试使用JSONP If the API is not supporting jsonp, you have to create a service which acts as a middleman between the API and your client. 如果API不支持jsonp,则必须创建一个服务,充当API和客户端之间的中间人。 In my case, i have created a asmx service. 就我而言,我创建了一个asmx服务。

sample below: 样品如下:

ajax call: ajax电话:

$(document).ready(function () {
        $.ajax({
            crossDomain: true,
            type:"GET",
            contentType: "application/json; charset=utf-8",
            async:false,
            url: "<your middle man service url here>/GetQuote?callback=?",
            data: { symbol: 'ctsh' },
            dataType: "jsonp",                
            jsonpCallback: 'fnsuccesscallback'
        });
    });

service (asmx) which will return jsonp: service(asmx)将返回jsonp:

[WebMethod]
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public void GetQuote(String symbol,string callback)
    {          

        WebProxy myProxy = new WebProxy("<proxy url here>", true);

        myProxy.Credentials = new System.Net.NetworkCredential("username", "password", "domain");
        StockQuoteProxy.StockQuote SQ = new StockQuoteProxy.StockQuote();
        SQ.Proxy = myProxy;
        String result = SQ.GetQuote(symbol);
        StringBuilder sb = new StringBuilder();
        JavaScriptSerializer js = new JavaScriptSerializer();
        sb.Append(callback + "(");
        sb.Append(js.Serialize(result));
        sb.Append(");");
        Context.Response.Clear();
        Context.Response.ContentType = "application/json";
        Context.Response.Write(sb.ToString());
        Context.Response.End();         
    }

At my work we have our restful services on a different port number and the data resides in db2 on a pair of AS400s. 在我的工作中,我们在不同的端口号上提供了宁静的服务,数据驻留在一对AS400上的db2中。 We typically use the $.getJSON AJAX method because it easily returns JSONP using the ?callback=? 我们通常使用$.getJSON AJAX方法,因为它使用?callback=?轻松返回JSONP ?callback=? without having any issues with CORS. 没有任何CORS问题。

data ='USER=<?echo trim($USER)?>' +
         '&QRYTYPE=' + $("input[name=QRYTYPE]:checked").val();

        //Call the REST program/method returns: JSONP 
        $.getJSON( "http://www.stackoverflow.com/rest/resttest?callback=?",data)
        .done(function( json ) {        

              //  loading...
                if ($.trim(json.ERROR) != '') {
                    $("#error-msg").text(message).show();
                }
                else{
                    $(".error").hide();
                    $("#jsonp").text(json.whatever);

                }

        })  
        .fail(function( jqXHR, textStatus, error ) {
        var err = textStatus + ", " + error;
        alert('Unable to Connect to Server.\n Try again Later.\n Request Failed: ' + err);
        });     

暂无
暂无

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

相关问题 带有jquery.ajax POST的Mturk externalQuestion:访问控制允许来源错误 - Mturk externalQuestion with jquery.ajax POST: Access-Control-Allow-Origin error jQuery Ajax不起作用-否&#39;Access-Control-Allow-Origin&#39; - Jquery ajax not working - No 'Access-Control-Allow-Origin' jQuery Ajax登录中没有“ Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' Header In jQuery Ajax Login Ajax-“访问控制允许来源”错误 - Ajax - “Access-Control-Allow-Origin” Error Jquery (jfeed) - Access-Control-Allow-Origin 不允许来源 xxxxx - Jquery (jfeed) - Origin xxxxx is not allowed by Access-Control-Allow-Origin Access-Control-Allow-Origin不允许使用Origin null(同步,无jQuery) - Origin null is not allowed by Access-Control-Allow-Origin (Synchronous, no jQuery) 服务器响应状态为401-无“ Access-Control-Allow-Origin”标头-Angular Js-与Jquery Ajax配合正常 - Server responded with a status of 401 - No 'Access-Control-Allow-Origin' header - Angular Js - working fine with Jquery Ajax 无法使此jQuery AJAX + JSONP正常工作。 “不存在&#39;Access-Control-Allow-Origin&#39;标头” - Cannot get this jQuery AJAX + JSONP to work. “No 'Access-Control-Allow-Origin' header is present” Access-Control-Allow-Origin不允许将jQuery Ajax POST到Rails 3.2.2 - jQuery Ajax POST to Rails 3.2.2 not allowed by Access-Control-Allow-Origin AJAX请求中不存在“ access-control-allow-origin”标头 - No 'access-control-allow-origin' header is present in AJAX request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM