Show some error Uncaught ReferenceError:

后端 未结 2 459
半阙折子戏
半阙折子戏 2020-12-11 10:41

HI i have call a json file and show some error can u please help me

show error Uncaught ReferenceError: marketlivedata is not defined<

相关标签:
2条回答
  • 2020-12-11 11:16

    The returned data is marketlivedata(...). This is calling the marketlivedata function, which is not defined in your script. Since, you've used dataType as jsonp, the function is executed.

    To solve this you can change the data format from the JSON server(which might not be possible as this looks like third party service) or you can define a function of that name which will be called when the response has arrived.

    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);
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

    0 讨论(0)
  • 2020-12-11 11:25

    As you are using json data which comes from another domain so there would be a callback function needed to access this:

    Uncaught ReferenceError: marketlivedata is not defined

    Here marketlivedata is a Callback Wrapper function which is returned from the service you are hitting, so Either a reference of a global function has to be set with the name of marketlivedata or make use of jsonpCallback: 'callback'(much better one) where "callback" is the function from the response. This function is actually carrying the data which you want to use, So this has to be done:

    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();
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    0 讨论(0)
提交回复
热议问题