我一直在努力让这个js脚本起作用。
我一直得到它的响应" XMLHttpRequest无法加载" " No' Access-Control-Allow-Origin'标头出现在请求的资源上。起源' http://localhost'因此不允许访问。"
我尝试添加一个Access-Control-Allow-Origin标头,但后来说它在所请求的资源上不存在
注意:页面上的评论是因为这是关闭另一个网站
任何帮助?
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$.ajax({
// The 'type' property sets the HTTP method.
// A value of 'PUT' or 'DELETE' will trigger a preflight request.
type: 'GET',
// The URL to make the request to.
url: 'http://globalcurrencies.xignite.com/xGlobalCurrencies.json/GetRealTimeRate?Symbol=EURUSD&_token=28ADDE2CAE3C4F2AB369E9ACDEF214AA',
Host: '127.0.0.1',
dataType : 'json',
// The 'contentType' property sets the 'Content-Type' header.
// The JQuery default for this property is
// 'application/x-www-form-urlencoded; charset=UTF-8', which does not trigger
// a preflight. If you set this value to anything other than
// application/x-www-form-urlencoded, multipart/form-data, or text/plain,
// you will trigger a preflight request.
contentType: 'application/x-www-form-urlencoded',
xhrFields: {
// The 'xhrFields' property sets additional fields on the XMLHttpRequest.
// This can be used to set the 'withCredentials' property.
// Set the value to 'true' if you'd like to pass cookies to the server.
// If this is enabled, your server must respond with the header
//Access-Control-Allow-Credentials: false
//Access-Control-Allow-origin: true,
//withCredentials: false
},
headers: {
//Access-Control-Allow-origin: 'http://127.0.0.1',
// Set any custom headers here.
// If you set any non-simple headers, your server must include these
// headers in the 'Access-Control-Allow-Headers' response header.
},
success: function() {
// Here's where you handle a successful response.
alert( "works" );
},
error: function() {
alert( "failed" );
// Here's where you handle an error response.
// Note that if the error was due to a CORS issue,
// this function will still fire, but there won't be any additional
// information about the error.
}
});
&#13;
答案 0 :(得分:0)
http://www.w3resource.com/JSON/JSONP.php指出,由于“同源策略”,我们需要使用JSONP从驻留在不同域中的服务器请求数据。上面的页面列出了执行POST的三个步骤。要调用Xignite,请添加到您的查询字符串:&amp; _callback = yourFunction(请参阅http://www.xignite.com/Support/FAQ.aspx?faqtype=Topics&faqcat=API_Integration#ka0400000008Rl8AAE)。 使用GET的HTML页面看起来应该是这样的(对于GlobalQuotes API):
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<title>JSONP example</title>
</head>
<body>
<div id="output"></div>
<script>
var url = "http://globalquotes.xignite.com/v3/xGlobalQuotes.json/GetGlobalDelayedQuotes?Identifiers=avxs,pti,bgne,edit&IdentifierType=Symbol&_Token=28ADDE2CAE3C4F2AB369E9ACDEF214AA&_callback=foo";
var tag = document.createElement("script");
tag.src = url;
document.getElementsByTagName("head")[0].appendChild(tag);
function foo(response) {
debugger;
document.getElementById("output").innerHTML = response[0].Security.Symbol + ': ' + response[0].Last;
};
</script>
</body>
</html>