我一直在努力让这个js脚本起作用。
我一直得到这样的响应:"XMLHttpRequest无法加载“"No‘access-Control-Allow-原产地’报头出现在请求的资源上,因此不允许访问。”
我尝试过添加一个访问控制允许源头,但是它说在请求的资源上不存在一个。
注意:页面上的注释是因为这是在另一个站点之外的。
有什么帮助吗?
<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.
}
});
发布于 2016-03-14 17:24:03
http://www.w3resource.com/JSON/JSONP.php指出,由于“相同的原产地策略”,我们需要使用JSONP从位于不同域中的服务器请求数据。上面的页面列出了三个要发布的步骤。要调用Xignite,请添加到querystring:&_callback=yourFunction (参见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>
https://stackoverflow.com/questions/35191703
复制相似问题