首页 > jquery > 406当试图通过jQuery查询finance.yahoo.com/webservice(Yahoo Finance)时

406当试图通过jQuery查询finance.yahoo.com/webservice(Yahoo Finance)时 (406 when trying to query finance.yahoo.com/webservice (Yahoo Finance) through jQuery)

2017-01-02 jqueryjsonjsonp

问题

我试图通过Webservices从雅虎财经获取数据。我想查询这个来源。当我把它放入我的浏览器地址栏时,一切正常:

网址:

http://finance.yahoo.com/webservice/v1/symbols/DAI.DE,ALIK16.CMX/quote?format=json

结果:

{
  "list": {
    "meta": {
      "type": "resource-list",
      "start": 0,
      "count": 2
    },
    "resources": [
      {
        "resource": {
          "classname": "Quote",
          "fields": {
            "name": "DAIMLER N",
            "price": "54.439999",
            "symbol": "DAI.DE",
            "ts": "1467029449",
            "type": "equity",
            "utctime": "2016-06-27T12:10:49+0000",
            "volume": "3990557"
          }
        }
      },
      {
        "resource": {
          "classname": "Quote",
          "fields": {
            "name": "Aluminum Futures,May-2016",
            "price": "1600.250000",
            "symbol": "ALIK16.CMX",
            "ts": "1464022420",
            "type": "future",
            "utctime": "2016-05-23T16:53:40+0000",
            "volume": "3"
          }
        }
      }
    ]
  }
}

当我尝试在一个简单的HTML页面中实现这个qith jQuery时,我总是会遇到错误406。我正在寻找几个小时没有任何解决方案。我究竟做错了什么?

这是我的HTML页面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Ohne_Titel_1</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript">

$.ajax({
  url: "http://finance.yahoo.com/webservice/v1/symbols/DAI.DE,ALIK16.CMX/quote?format=json",
  dataType: 'jsonp',  //use jsonp data type in order to perform cross domain ajax
  crossDomain: true,
    success: function(json) {
    console.log(" reponse :"+ json);
    },error: function (XMLHttpRequest, textStatus, errorThrown) {
    console.log("error :"+XMLHttpRequest.responseText);
    }
});

</script>
</head>

<body>
    <div id='dvStockTicker' class='stockTicker'></div>
</body>

</html>

解决方法

这是一个老问题,但万一你偶然发现:似乎Yahoo改变了这个API的使用条款。似乎只允许从手机上使用,而不是从桌面上。有关解决方法,请参阅:雅虎财务网络服务是否已消失?API改变了吗?暂时下来?

问题

I am trying to get data from Yahoo Finance via the Webservices. I want to query this source. When i put this into my browsers address bar then everything works fine:

url:

http://finance.yahoo.com/webservice/v1/symbols/DAI.DE,ALIK16.CMX/quote?format=json

result:

{
  "list": {
    "meta": {
      "type": "resource-list",
      "start": 0,
      "count": 2
    },
    "resources": [
      {
        "resource": {
          "classname": "Quote",
          "fields": {
            "name": "DAIMLER N",
            "price": "54.439999",
            "symbol": "DAI.DE",
            "ts": "1467029449",
            "type": "equity",
            "utctime": "2016-06-27T12:10:49+0000",
            "volume": "3990557"
          }
        }
      },
      {
        "resource": {
          "classname": "Quote",
          "fields": {
            "name": "Aluminum Futures,May-2016",
            "price": "1600.250000",
            "symbol": "ALIK16.CMX",
            "ts": "1464022420",
            "type": "future",
            "utctime": "2016-05-23T16:53:40+0000",
            "volume": "3"
          }
        }
      }
    ]
  }
}

When i try to implement this qith jQuery within a simple HTML page i always get an error 406. I am searching now for hours without any solution. What am i doing wrong?

This is my HTML page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Ohne_Titel_1</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript">

$.ajax({
  url: "http://finance.yahoo.com/webservice/v1/symbols/DAI.DE,ALIK16.CMX/quote?format=json",
  dataType: 'jsonp',  //use jsonp data type in order to perform cross domain ajax
  crossDomain: true,
    success: function(json) {
    console.log(" reponse :"+ json);
    },error: function (XMLHttpRequest, textStatus, errorThrown) {
    console.log("error :"+XMLHttpRequest.responseText);
    }
});

</script>
</head>

<body>
    <div id='dvStockTicker' class='stockTicker'></div>
</body>

</html>

解决方法

This is an old question, but just in case you stumbled here: Seems Yahoo changed who the usage terms for this API. Seems to be permissible from phones only, not from desktop. For a workaround, see: Has Yahoo finance web service disappeared? API changed? Down temporarily?

相似信息