George_Fal 2017-09-17 20:57 采纳率: 0%
浏览 28

如何从Json获取数据

I' currently learning AJAX technology.

In Chrome console when I type jsonObject.responseText, I get all data in the console, but when I do the same in my .js file to print that to the console (or HTML element) it says "undefined" in the console.

JSON object is from:

https://jsonplaceholder.typicode.com/users

Code in JavaScript:

var jsonObject = $.ajax({
url: "https://jsonplaceholder.typicode.com/users",
dataType: "json"
});
console.log(jsonObject.responseText);
  • 写回答

2条回答 默认 最新

  • weixin_33709219 2017-09-17 21:00
    关注

    You should access the response you are getting from the ajax call inside the success /done handler of the call.

    $.ajax({
        url: "https://jsonplaceholder.typicode.com/users",
        dataType: "json"
        })
        .done(function(jsonObject){
           // Received response from the call. Now you can safely access it
           console.log(jsonObject);
       });
    

    Here is a working jsbin

    Callbacks specified in the done handler will be executed when the ajax call successfully receives a response from the server.

    评论

报告相同问题?