Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用jquery从ajax结果(json对象)访问数据_Javascript_Jquery_Ajax - Fatal编程技术网

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

Javascript 如何使用jquery从ajax结果(json对象)访问数据,javascript,jquery,ajax,Javascript,Jquery,Ajax,所以我有这个函数,它通过UUID获取用户详细信息 function getUserDetails(uuid){ $.ajax({ url:'http://localhost/hrms/controller/php/getUserDetails.php', method: 'POST', dataType: 'json', data: { uuid: uuid }, success:function(result){

所以我有这个函数,它通过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);
        }
      }
    });
}
这给了我一个结果

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


我正在使用JQUERY。谢谢,还有更多功能。

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

回调:

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);
        }
      }
    });
 }
getUserDetails(uuid, function(data){
    console.log(data); 
}); 
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);
      }
    });
   });
 }
getUserDetails(uuid).then(function(data){
   console.log(data); 
});
index.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);
        }
      }
    });
 }
getUserDetails(uuid, function(data){
    console.log(data); 
}); 
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);
      }
    });
   });
 }
getUserDetails(uuid).then(function(data){
   console.log(data); 
});
承诺

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);
        }
      }
    });
 }
getUserDetails(uuid, function(data){
    console.log(data); 
}); 
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);
      }
    });
   });
 }
getUserDetails(uuid).then(function(data){
   console.log(data); 
});
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);
        }
      }
    });
 }
getUserDetails(uuid, function(data){
    console.log(data); 
}); 
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);
      }
    });
   });
 }
getUserDetails(uuid).then(function(data){
   console.log(data); 
});
index.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);
        }
      }
    });
 }
getUserDetails(uuid, function(data){
    console.log(data); 
}); 
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);
      }
    });
   });
 }
getUserDetails(uuid).then(function(data){
   console.log(data); 
});

你说“函数来自不同的js页面”是什么意思?我有functions.js,函数是在这里编写的。我通过另一个js文件(如index.js)访问函数。我尝试使用数组回调,但当我尝试console.log时,我得到了未定义的结果。我做错了什么?还是不明白你说的First.js
function function name(){…}
Second.js
var data=function name()
类似的东西。