2016-10-26 15 views
0

私はAPIからの応答を解析しようとしています。私は全体の応答を得ることができますが、私は実際にそれを解析する方法を混乱させました。ここでMarkit On Demand APIの結果を解析する - JavaScript、AJAX

は私のコードです:ここでは

<!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> 

は、私が現在受信しています応答である:http://dev.markitondemand.com/MODApis/

:ここ

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 
} 
) 

は、オンデマンドAPIでマークイットへのリンクです

注:ChromeとFirefoxで、次のエラーが表示されます。 XMLHttpRequestはhttp://dev.markitondemand.com/Api/v2/Quote/jsonp?symbol=AAPLをロードできません。要求されたリソースに「Access-Control-Allow-Origin」ヘッダーが存在しません。したがって、原点「ヌル」はアクセスが許可されません。

これを解決するには何が必要ですか?

答えて

0

あなたの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> 
使用しています
0

キー値のペアを戻しています。あなたが好きなら、JSON.parseで解析できます。

これはJavaScriptオブジェクトをその文字列から見た文字列から除外します。その後、簡単にコンテンツにアクセスできます。

alert(Object.info); 
関連する問題