显示一些错误未捕获的ReferenceError:

时间:2015-09-08 06:28:58

标签: javascript jquery json

我已经打电话给 json 文件并显示一些错误,请你帮帮我

显示错误Uncaught ReferenceError: marketlivedata is not defined

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!DOCTYPE html>
<html xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:g="http://base.google.com/ns/1.0">

<head>
  <title>Data Call to json</title>



  <script type="text/javascript">
    // =====================
    $(document).ready(function(e) {
      //    alert('hello');
      //  var marketlivedata ;
    });
     // =====================
    function getUserData() {
      $.ajax({
        type: "GET",
        url: "http://xxxxxxxxx.xxxxxxx.com/xxxxxxx.json",
        dataType: 'jsonp',
        crossDomain: true,
        success: function(data) {
          //  $('#ajexLoaderSec').hide();
          console.log(data);

        },
        error: function(e) {
          alert("There is an error while connecting to the server. Please try after some time");
        }
      });
    };
    getUserData();
  </script>
</head>

<body>
  gsdf sdf sdfsd sdf sd
</body>

</html>

我的json格式是

marketlivedata([{"sensex":{"trend":"equal","CloseIndexValue":"24893.81","premarket":"false","DateTime":"11:41 AM | 08 Sep 2015","CurrentIndexValue":"24958.31","Segment":"BSE","OpenIndexValue":"24972.01","PercentChange":"0.26","IndexName":"SENSEX","NetChange":"64.50"},"nifty":{"trend":"equal","CloseIndexValue":"7558.80","premarket":"false","DateTime":"11:41 AM | 08 Sep 2015","CurrentIndexValue":"7582.85","Segment":"NSE","OpenIndexValue":"7587.70","PercentChange":"0.32","IndexName":"CNX NIFTY","NetChange":"24.05"},"gold":{"ClosePrice":"26500.00","trend":"negative","OpenPrice":"26499.00","ExpiryDate":"2015-10-05","SpotSymbol":"SGOLDAHM","LastTradedPrice":"26441.00","DateTime":"8-September-2015 11:34:22","Symbol":"GOLD","PercentChange":"-0.22","CommodityName":"Gold","NetChange":"-59.00","PriceQuotationUnit":"10 GRMS ","SpotPrice":"26401.0"},"silver":{"ClosePrice":"35193.00","trend":"equal","OpenPrice":"35176.00","ExpiryDate":"2015-12-04","SpotSymbol":"SSILVERAHM","LastTradedPrice":"35070.00","DateTime":"8-September-2015 11:34:0","Symbol":"SILVER","PercentChange":"-0.35","CommodityName":"Silver","NetChange":"-123.00","PriceQuotationUnit":"1 KGS  ","SpotPrice":"34815.0"},"USD/INR":{"DateTime":"2015-09-08 11:36:02.0","percentChange":"-0.27","netChange":"-0.18","name":"USD/INR","bidprice":"66.65"},"DXY Index":{"DateTime":"2015-09-08 11:39:40.0","percentChange":"-0.1","netChange":"-0.1","name":"DXY Index","bidprice":"96.13"},"marketstatus":{"currentMarketStatus":"Live"}}])

2 个答案:

答案 0 :(得分:2)

返回的数据为marketlivedata(...)。这是调用marketlivedata函数,该函数未在脚本中定义。由于您已将dataType用作jsonp,因此执行该函数。

要解决此问题,您可以从JSON服务器更改数据格式(这可能不可能,因为这看起来像第三方服务),或者您可以定义该名称的函数,该函数将在响应到达时被调用。 / p>

&#13;
&#13;
function getUserData() {
  $.ajax({
    type: "GET",
    url: "http://mobilelivefeeds.indiatimes.com/homepagedatanew.json",
    dataType: 'jsonp',
    crossDomain: true,
    success: function(data) {
      //  $('#ajexLoaderSec').hide();
      console.log(data);

    },
    error: function(e) {
      console.log(e);
    }
  });
};
getUserData();


function marketlivedata(data) {
  console.log(data);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

当您使用来自其他域的json数据时,将需要一个回调函数来访问它:

未捕获的ReferenceError:未定义marketlivedata

此处marketlivedata是一个 Callback Wrapper 函数,它是从您正在访问的服务返回的,因此必须使用名称{{1来设置全局函数的引用或者使用marketlivedata (更好的一个)其中jsonpCallback: 'callback'是响应中的函数。这个函数实际上是要携带你想要使用的数据,所以必须这样做:

&#13;
&#13;
"callback"
&#13;
function getUserData() {
  $.ajax({
    type: "GET",
    url: "http://mobilelivefeeds.indiatimes.com/homepagedatanew.json",
    dataType: 'jsonp',
    crossDomain: true,
    jsonpCallback: 'marketlivedata', // call the returned function here.
    success: function(data) {
           document.body.innerHTML = '<pre>' + JSON.stringify(data) + '</pre>'; // use the data as you need to.
    }, // you can refere it here
    error: function(e) {
      console.log(e);
    }
  });
};
getUserData();
&#13;
&#13;
&#13;