我试图解析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>
更多推荐
发布评论