简体   繁体   English

我怎样才能从这个 AJAX 调用返回结果?

[英]How can I return the result from this AJAX call?

I have the following JScode我有以下 JScode

 function DetermineLoggedUser(){ return $.post('determineLoggedUser.php',{ }).then((result) => { loggedUser = result; })

The php looks like this: php 看起来是这样的:

 <?php session_start() if(ISSET($_SESSION["loggedUser"])) { echo $_SESSION["loggedUser"]; }else{ echo "'userNotLogged'"; } ?>

Now, I want DetermineLoggedUser() to return the value of loggedUser after it has been set by $.post AJAX call.现在,我希望DetermineLoggedUser() loggedUser DetermineLoggedUser()在$.post AJAX 调用设置后返回loggedUser的值。 At the same time, I want the function calling DetermineLoggedUser() to wait, using async/await.同时,我希望调用DetermineLoggedUser()的函数使用async/await 等待。 So it would look kinda like this:所以它看起来有点像这样:

 async function callingSeveralFunctions(){ //some functions var result = await determineLoggedUser(); //some other functions which need to wait for determineLoggedUser() } function DetermineLoggedUser(){ return $.post('determineLoggedUser.php',{ }).then((result) => { loggedUser = result; }) callingSeveralFunctions();

So, since I need to return the promise created by the AJAX call in order to make "await" work, I wonder how I can at the same time return the value set to loggedUser inside determineLoggedUser() ?因此,由于我需要返回由 AJAX 调用创建的承诺才能使“await”工作,我想知道如何同时返回设置到determineLoggedUser() loggedUser determineLoggedUser()内的loggedUser的值?

You've got the first bit right - you're correct in returning the Promise from your DetermineLoggedUser() function.您的第一点是正确的 - 从您的DetermineLoggedUser()函数返回 Promise 是正确的。 The calling code can then attach a .then() to that Promise object (as opposed to attaching it within the DetermineLoggedUser function, which isn't so useful) in which you can retrieve the value, and then execute whatever other functions you need which depend on it.然后,调用代码可以将.then()附加到该 Promise 对象(而不是将它附加到不是很有用的 DeleteLoggedUser 函数中),您可以在其中检索值,然后执行您需要的任何其他函数依赖它。

function callingSeveralFunctions(){
  //some functions
  var promise = determineLoggedUser();
  promise.then((response) => {
    loggedUser = response;
    //some other functions which need to wait for determineLoggedUser() - calls to these need to go inside the callback, here
  });
}

function DetermineLoggedUser(){
  return $.post('determineLoggedUser.php',{});
}

callingSeveralFunctions();

You can't escape the fact that it's asynchronous, but you can work with it better.您无法逃避它是异步的这一事实,但您可以更好地使用它。

Each promise, need to return something, in your case, then you call you file determineLoggedUser.php he go directly into your then function, expected that you return nothing.每个承诺,都需要返回一些东西,在你的情况下,然后你调用你的文件determineLoggedUser.php他直接进入你的then函数,期望你什么都不返回。

Try something like that :尝试这样的事情:

 const ajax = $.post('determineLoggedUser.php',{ }).then(result => { return res.json(); // In case of backend return json... }).then(data => { return console.log(data); }); console.log(ajax)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM