javascript
  • json
  • google-chrome
  • yahoo-finance
  • google-finance
  • 2016-08-17 42 views -2 likes 
    -2

    这是我的JS代码。这在IE中完美工作。如果我在浏览器中粘贴请求链接,那么我确实会得到JSON回复,但是当我运行此代码时。我没有收到任何雅虎或Google请求的回复。虽然在IE中工作得很好。Yahoo/Google财务JSON不能在Chrome中工作

    //var url1 = "http://finance.google.com/finance/info?client=ig&q=AAPL"; 
    var yah_url1 = 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20("'; 
    var yah_url2 = '")%0A%09%09&format=json&diagnostics=true&env=http%3A%2F%2Fdatatables.org%2Falltables.env&callback='; 
    
    function btn_stocks_click() 
    { 
        var div_table = document.getElementById("div_stock_table"); 
        var btn_stocks = document.getElementById("btn_stocks"); 
        div_table.style.display = (div_table.style.display == 'none') ? 'block' : 'none'; 
        btn_stocks.value = (btn_stocks.value == "Show Stocks") ? "Hide Stocks" : "Show Stocks"; 
        getJSONReply("AAPL"); 
    } 
    
    function getJSONReply() 
    { 
        var req = yah_url1.concat(arguments[0]); 
        var url_req = req.concat(yah_url2); 
        alert(url_req); 
        var xhr = new XMLHttpRequest(); 
        xhr.onreadystatechange = function() 
        { 
         if (xhr.readyState == 4 && xhr.status == 200) 
         { 
          alert(xhr.responseText.length); 
         } 
        } 
        xhr.open('GET', url_req, true); 
        xhr.setRequestHeader('Access-Control-Allow-Headers', '*'); 
        xhr.setRequestHeader('Access-Control-Allow-Origin', '*'); 
        xhr.setRequestHeader('Access-Control-Allow-Methods', 'GET'); 
        xhr.addEventListener("load", reqListener); 
        xhr.send(); 
    } 
    
    function reqListener() // This was coded for Google Finance reply as it had other characters apart from reply. 
    { 
        var sub1 = this.responseText.substring(5,this.responseText.length); 
        var sub2 = sub1.substring(0, sub1.length - 2); 
        parse_JSON(sub2); 
    } 
    
    function parse_JSON() 
    { 
        var response = arguments[0]; 
        alert(arguments[0]); 
    } 
    

    这是越来越显示错误在Chrome调试器

    的XMLHttpRequest不能加载http://query.yahooapis.com/v1/public/yql?q=select%20%2a%20from%20yahoo.finance.quotes%20where%20symbol%20in%20%28%22AAPL%22%29%0A%09%09&env=http%3A%2F%2Fdatatables.org%2Falltables.env&format=json响应预检请求未通过访问控制检查:否“访问控制允许来源”标题存在于请求的资源。因此不允许原产地'null'访问。

    +0

    的XMLHttpRequest不能加载 'http://query.yahooapis.com/v1/public/yql?q=select %20%2a%20%从%20yahoo.fina ... 22%29%0A%09%09&env = http%3A%2F%2Fdatatables.org%2Falltables.env&format = json.' 对预检请求的响应不会传递访问控制检查:请求的资源上是否存在“Access-Control-Allow-Origin”标题。因此不允许原产地'null'访问。 –

    +0

    请将它置于您的问题中。使用报价格式。 – isherwood

    回答

    0

    我有使用WebClient替代的解决方案,调用此方法使用AJAX货币转换

     public JsonResult convertCurrencyUsingGoogle(decimal amount, string from, string to) 
         { 
          try 
          { 
           WebClient WebClient = new WebClient(); 
           from = from.ToUpper(); 
           to = to.ToUpper(); 
    
           try 
           { 
    
            var CurrencyQuery = new StringBuilder(); 
            CurrencyQuery.Append("https://www.google.com/finance/converter"); 
            CurrencyQuery.AppendFormat("?a={0}", amount); 
            CurrencyQuery.AppendFormat("&from={0}", from); 
            CurrencyQuery.AppendFormat("&to={0}", to); 
    
            string response = WebClient.DownloadString(CurrencyQuery.ToString()); //response returns as page 
            var responsestring = response.Split(new string[] { "class=bld>" }, StringSplitOptions.None); // get element which contains money 
            var finalresponse = responsestring[1].Split(new string[] { to }, StringSplitOptions.None); // split curreny type 
    
            return Json(String.Format("{0:G29}", Convert.ToDecimal(finalresponse[0].Trim()))); //return currency 
           } 
           catch (Exception e) 
           { 
            return Json("error"); 
           } 
          } 
          catch (Exception e) 
          { 
           return Json("error"); 
          } 
         } 
    
    +0

    在评论中提及原因,之前不一样 – Arjun

    相关问题