jquery.ajax Access-Control-Allow-Origin
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19085965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery.ajax Access-Control-Allow-Origin
提问by dwarf
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. Its a call to sportsdata's api. On the site, they show you a request header of x-originating-ip so i have tried that where access-control-allow-originis.
我的网址是合法的。你会注意到我没有数据集。我不确定数据类型是否在返回实际 xml 时正常工作,但我也尝试过。它是对sportsdata 的api 的调用。在网站上,他们向您展示了 x-originating-ip 的请求标头,因此我尝试了access-control-allow-origin所在的位置。
All of this still returned the access-controlerror. 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
所有这些仍然返回访问控制错误。我不清楚如果我设置它是什么数据,所以我现在省略了它。我尝试了一些不同的东西,我用谷歌搜索,我明白为什么我会收到错误。我不知道如何解决它。我试着不用问,但如果有人能解释或给我指路,那将不胜感激
回答by Saranya
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. If the API is not supporting jsonp, you have to create a service which acts as a middleman between the API and your client. In my case, i have created a asmx service.
您可以尝试使用JSONP。如果 API 不支持 jsonp,您必须创建一个服务作为 API 和您的客户端之间的中间人。就我而言,我创建了一个 asmx 服务。
sample below:
示例如下:
ajax call:
阿贾克斯调用:
$(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:
将返回 jsonp 的服务 (asmx):
[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();
}
回答by yardpenalty.com
At my work we have our restful services on a different port number and the data resides in db2 on a pair of AS400s. We typically use the $.getJSON
AJAX method because it easily returns JSONP using the ?callback=?
without having any issues with CORS.
在我的工作中,我们的 Restful 服务位于不同的端口号上,数据驻留在一对 AS400 上的 db2 中。我们通常使用$.getJSON
AJAX 方法,因为它可以使用 轻松返回 JSONP,?callback=?
而不会出现任何 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);
});