jquery.ajax访问控制 - 允许 - 原产地 [英] 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'
});
我的网址是合法的。你会发现,我没有数据集。我不能确定,如果数据类型是否正常工作作为返回其实际的XML,但我想这一点。它调用sportsdata的API。在网站上,他们表现出你的x始发-IP,所以我已经试过了,其中的访问控制 - 允许原产地是一个请求头。
这一切都还是返回访问控制错误。我不清楚是什么数据是,如果我将它设置,所以我省略它现在。我已经尝试了一些不同的事情,我GOOGLE了,我明白了为什么我收到错误。我不知道如何解决它。我想不必问,但如果有人能解释或告诉我的方式,那将是极大的AP preciated
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
推荐答案
<一个href="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.
您可以尝试使用 JSONP 。如果API不支持JSONP,你必须创建它充当API和您的客户之间的中间人的服务。就我而言,我创建了一个ASMX服务。
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.
下面的示例:
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'
});
});
服务(ASMX),这将返回JSONP:
service (asmx) which will return 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();
}
这篇关于jquery.ajax访问控制 - 允许 - 原产地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!