如何使用jquery从ajax结果(json对象)访问数据

时间:2023-01-26 10:07:21

So I have this function that gets the user details via UUID;

所以我有这个函数通过UUID获取用户的详细信息;

function getUserDetails(uuid){

    $.ajax({
      url:'http://localhost/hrms/controller/php/getUserDetails.php',
      method: 'POST',
      dataType: 'json',
      data: { uuid: uuid },
      success:function(result){
        console.log(result);
      },
      error: function(xhr, status, error){
        if(xhr.status == 401){
          toastr.error(xhr.responseText);
        }
      }
    });
}

which give me the result of

这给了我的结果

AJAX RESULT

AJAX结果

Now I wonder how to access that data. The function is from different js page. How can I throw the json result to another js page?

现在我想知道如何访问这些数据。该函数来自不同的js页面。如何将json结果抛出到另一个js页面?

Im using JQUERY. Thanks and more powers.

我正在使用JQUERY。谢谢和更多的权力。

1 个解决方案

#1


-1  

When you use ajax it takes some time to return the data form server to client, so you can use promise or callbacks:

当您使用ajax时,将数据表单服务器返回给客户端需要一些时间,因此您可以使用promise或callback:

Callbacks:

回调:

function.js

function.js

function getUserDetails(uuid, callback){

    $.ajax({
      url:'http://localhost/hrms/controller/php/getUserDetails.php',
      method: 'POST',
      dataType: 'json',
      data: { uuid: uuid },
      success:function(result){
        callback(result);
      },
      error: function(xhr, status, error){
        if(xhr.status == 401){
          toastr.error(xhr.responseText);
        }
      }
    });
 }

index.js

index.js

getUserDetails(uuid, function(data){
    console.log(data); 
}); 

Promises

承诺

function.js

function.js

function getUserDetails(uuid){
 return new Promise((resolve, reject) => {
    $.ajax({
      url:'http://localhost/hrms/controller/php/getUserDetails.php',
      method: 'POST',
      dataType: 'json',
      data: { uuid: uuid },
      success:function(result){
        resolve(result);
      },
      error: function(xhr, status, error){
        if(xhr.status == 401){
          toastr.error(xhr.responseText);
        }
        const reason = new Error('Error');
        reject(reason);
      }
    });
   });
 }

index.js

index.js

getUserDetails(uuid).then(function(data){
   console.log(data); 
});

#1


-1  

When you use ajax it takes some time to return the data form server to client, so you can use promise or callbacks:

当您使用ajax时,将数据表单服务器返回给客户端需要一些时间,因此您可以使用promise或callback:

Callbacks:

回调:

function.js

function.js

function getUserDetails(uuid, callback){

    $.ajax({
      url:'http://localhost/hrms/controller/php/getUserDetails.php',
      method: 'POST',
      dataType: 'json',
      data: { uuid: uuid },
      success:function(result){
        callback(result);
      },
      error: function(xhr, status, error){
        if(xhr.status == 401){
          toastr.error(xhr.responseText);
        }
      }
    });
 }

index.js

index.js

getUserDetails(uuid, function(data){
    console.log(data); 
}); 

Promises

承诺

function.js

function.js

function getUserDetails(uuid){
 return new Promise((resolve, reject) => {
    $.ajax({
      url:'http://localhost/hrms/controller/php/getUserDetails.php',
      method: 'POST',
      dataType: 'json',
      data: { uuid: uuid },
      success:function(result){
        resolve(result);
      },
      error: function(xhr, status, error){
        if(xhr.status == 401){
          toastr.error(xhr.responseText);
        }
        const reason = new Error('Error');
        reject(reason);
      }
    });
   });
 }

index.js

index.js

getUserDetails(uuid).then(function(data){
   console.log(data); 
});