首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用xignite api检索库存数据

使用xignite api检索库存数据
EN

Stack Overflow用户
提问于 2016-02-04 03:00:31
回答 1查看 312关注 0票数 0

我一直在努力让这个js脚本起作用。

我一直得到这样的响应:"XMLHttpRequest无法加载“"No‘access-Control-Allow-原产地’报头出现在请求的资源上,因此不允许访问。”

我尝试过添加一个访问控制允许源头,但是它说在请求的资源上不存在一个。

注意:页面上的注释是因为这是在另一个站点之外的。

有什么帮助吗?

代码语言:javascript
复制
<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.
  }
});

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-14 17:24:03

http://www.w3resource.com/JSON/JSONP.php指出,由于“相同的原产地策略”,我们需要使用JSONP从位于不同域中的服务器请求数据。上面的页面列出了三个要发布的步骤。要调用Xignite,请添加到querystring:&_callback=yourFunction (参见Integration#ka0400000008Rl8AAE)。带有GET的HTML应该如下所示(对于GlobalQuotes API):

代码语言:javascript
复制
<!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>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35191703

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档