解析Markit On Demand API的结果

编程入门 行业动态 更新时间:2024-10-28 06:32:28
解析Markit On Demand API的结果 - JavaScript,AJAX(Parse results from Markit On Demand API - JavaScript, AJAX)

我试图解析API的响应。 我可以得到整个回应,但我对如何实际解析它感到困惑。

这是我的代码:

<!DOCTYPE> <html> <head> <style> img { margin: 10px; width: 100px; } </style> </head> <body> <button type="button" class="btn">Click me!</button> <div class="text">Replace me!!</div> <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script> <script> $('.btn').click(function() { $('.text').text('loading . . .'); $.ajax({ type:"GET", //url: "http://dev.markitondemand.com/MODApis/Api/v2/Lookup/jsonp?input=NFLX", url: "http://dev.markitondemand.com/Api/v2/Quote/jsonp?symbol=AAPL", success: function(response) { $('.text').html(''); alert(response); alert(response.name); alert(response.Name); $('.text').text(JSON.stringify(response)); } }); }); </script> </body>

这是我目前收到的回复:

myFunction ( { "Status": "SUCCESS", "Name": "Apple Inc", "Symbol": "AAPL", "LastPrice": 117.65, "Change": 1.05000000000001, "ChangePercent": 0.900514579759873, "Timestamp": "Mon Oct 24 00:00:00 UTC-04:00 2016", "MSDate": 42667, "MarketCap": 633950318950, "Volume": 23538673, "ChangeYTD": 105.26, "ChangePercentYTD": 11.7708531255938, "High": 117.74, "Low": 117, "Open": 117.1 } )

以下是Markit On Demand API的链接: http : //dev.markitondemand.com/MODApis/

注意:我在按钮单击时在Chrome和Firefox中出现以下错误,XMLHttpRequest无法加载http://dev.markitondemand.com/Api/v2/Quote/jsonp?symbol=AAPL 。 请求的资源上没有“Access-Control-Allow-Origin”标题。 因此不允许原产地'null'访问。

为了解决这个问题,我需要做什么?

I'm attempting to parse the response from the API. I can get the whole response but I confused on how to actually parse it.

Here is my code:

<!DOCTYPE> <html> <head> <style> img { margin: 10px; width: 100px; } </style> </head> <body> <button type="button" class="btn">Click me!</button> <div class="text">Replace me!!</div> <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script> <script> $('.btn').click(function() { $('.text').text('loading . . .'); $.ajax({ type:"GET", //url: "http://dev.markitondemand.com/MODApis/Api/v2/Lookup/jsonp?input=NFLX", url: "http://dev.markitondemand.com/Api/v2/Quote/jsonp?symbol=AAPL", success: function(response) { $('.text').html(''); alert(response); alert(response.name); alert(response.Name); $('.text').text(JSON.stringify(response)); } }); }); </script> </body>

Here is a response I'm currently receiving:

myFunction ( { "Status": "SUCCESS", "Name": "Apple Inc", "Symbol": "AAPL", "LastPrice": 117.65, "Change": 1.05000000000001, "ChangePercent": 0.900514579759873, "Timestamp": "Mon Oct 24 00:00:00 UTC-04:00 2016", "MSDate": 42667, "MarketCap": 633950318950, "Volume": 23538673, "ChangeYTD": 105.26, "ChangePercentYTD": 11.7708531255938, "High": 117.74, "Low": 117, "Open": 117.1 } )

Here is a link to Markit On Demand API: http://dev.markitondemand.com/MODApis/

NOTE: I am getting the following error in Chrome and Firefox on the button click, XMLHttpRequest cannot load http://dev.markitondemand.com/Api/v2/Quote/jsonp?symbol=AAPL. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

What do I need to do in order to solve this?

最满意答案

您的Ajax jsonp调用必须具有dataType: "jsonp" & jsonp: "callback"

<!DOCTYPE>
<html>

<head>
  <style>
    img {
      margin: 10px;
      width: 100px;
    }
  </style>
  <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
</head>

<body>
  <button type="button" class="btn">Click me!</button>
  <div class="text">Replace me!!</div>


  <script>
    $('.btn').click(function() {

      $('.text').text('loading . . .');

      $.ajax({
        type: "GET",
        //url: "http://dev.markitondemand.com/MODApis/Api/v2/Lookup/jsonp?input=NFLX",
        jsonp: "callback",
        dataType: "jsonp",
        url: "http://dev.markitondemand.com/Api/v2/Quote/jsonp?symbol=AAPL",
        success: function(response) {
          alert(response.Name);
          alert(response.Status);
          $('.text').html('');
          $('.text').text(JSON.stringify(response));
        }
      });
    });
  </script>
</body> 
  
 

或者使用纯粹的JavaScript

<script src="http://dev.markitondemand.com/MODApis/Api/v2/Quote/jsonp?symbol=AAPL&callback=workingWithJson"/> <script> function workingWithJson(data){ alert(data.Name); } </script>

Your Ajax jsonp call must has dataType: "jsonp" & jsonp: "callback"

<!DOCTYPE>
<html>

<head>
  <style>
    img {
      margin: 10px;
      width: 100px;
    }
  </style>
  <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
</head>

<body>
  <button type="button" class="btn">Click me!</button>
  <div class="text">Replace me!!</div>


  <script>
    $('.btn').click(function() {

      $('.text').text('loading . . .');

      $.ajax({
        type: "GET",
        //url: "http://dev.markitondemand.com/MODApis/Api/v2/Lookup/jsonp?input=NFLX",
        jsonp: "callback",
        dataType: "jsonp",
        url: "http://dev.markitondemand.com/Api/v2/Quote/jsonp?symbol=AAPL",
        success: function(response) {
          alert(response.Name);
          alert(response.Status);
          $('.text').html('');
          $('.text').text(JSON.stringify(response));
        }
      });
    });
  </script>
</body> 
  
 

Or using Pure JavaScript

<script src="http://dev.markitondemand.com/MODApis/Api/v2/Quote/jsonp?symbol=AAPL&callback=workingWithJson"/> <script> function workingWithJson(data){ alert(data.Name); } </script>

更多推荐

本文发布于:2023-07-20 20:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1203655.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Markit   Demand   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!