2015-04-29 49 views
0

如何追加股票價格和其他信息返回給Div?AJAX請求返回沒有'Access-Control-Allow-Origin'標頭出現在請求的資源上。 Origin

下面是代碼:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> 
<script> 

    function getResults(){ 
     alert(); 
     $.get("http://dev.markitondemand.com/Api/v2/Quote/jsonp?symbol=AAPL", function(data) { 
      document.getElementById("myDiv").innerHTML = data; 
      alert("Load was performed."); 
      }); 
    } 

</script> 
<head></head> 
<body> 
    <h2>Click here to start</h2> 
    <button type="button" onclick="getResults()">Request Price</button> 
    <div id="myDiv"> 
    </div> 

</body> 

回答

1

這是JSONP,所以你應該能夠把它拿來跨域,但似乎jQuery的需要有具體的數據類型集,所以這樣做應該工作

function getResults(){ 
 
    $.ajax({ 
 
    url : 'http://dev.markitondemand.com/Api/v2/Quote/jsonp', 
 
    data : {symbol : "AAPL"}, 
 
    type : 'GET', 
 
    dataType : 'jsonp' 
 
    }).done(function(data) { 
 
    document.getElementById("myDiv").innerHTML = JSON.stringify(data); 
 
    }); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 

 
<h2>Click here to start</h2> 
 
<button type="button" onclick="getResults()">Request Price</button> 
 
<div id="myDiv"></div>

FIDDLE

相關問題