从ajax函数中获取响应结果

时间:2017-07-29 12:33:05

标签: jquery json ajax

如何获取响应变量中的数据。在调试器中,我可以看到发回的所有数据(图像)但是如何从Object获取信息?

function getUserList() {
    $.ajax({
        url: "https://reqres.in/api/users",
        type: "POST",
        data: {
            name: "paul rudd",
            movies: ["I Love You Man", "Role Models"]
        },
        success: function(response) {

            console.log(response);

        }
    });

}

我正在关注

的例子

https://reqres.in/

enter image description here

1 个答案:

答案 0 :(得分:0)

以所需格式创建全局变量,并为其指定ajax post值。但首先确保对象属性然后创建对象。否则,您需要使用jquery extend()方法

来扩展它
var userObject = {createdAt:"",id:"",movies:[],name:""};

function getUserList() {

$.ajax({
    url: "https://reqres.in/api/users",
    type: "POST",
    data: {
        name: "paul rudd",
        movies: ["I Love You Man", "Role Models"]
    },
    success: function(response){
        userObject = response;
        console.log(userObject.createdAt+" "+ userObject.id);

    }
});