【问题标题】:AJAX Request returning No 'Access-Control-Allow-Origin' header is present on the requested resource. OriginAJAX 请求返回请求的资源上不存在“Access-Control-Allow-Origin”标头。起源
【发布时间】:2015-04-30 07:05:51
【问题描述】:

如何将返回的股票价格和其他信息附加到 Div?

代码如下:

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

【问题讨论】:

    标签: jquery ajax cross-browser


    【解决方案1】:

    它是 JSONP,所以你应该能够跨域获取它,但似乎 jQuery 需要设置 dataType,所以这样做应该可以工作

    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

    【讨论】:

      猜你喜欢
      • 2018-07-09
      • 1970-01-01
      • 2018-03-09
      • 2018-05-11
      • 2020-07-10
      • 2016-06-30
      • 2016-10-08
      相关资源
      最近更新 更多