无法从jQuery AJAX Success函数返回

时间:2022-10-09 11:33:00

Hey guys, I'm building an application which requires access to the server time before sending AJAX requests off, the only way to do this is to be able to have a function that requests the time from the server using AJAX, so in my function I have the following code:

嘿伙计们,我正在构建一个需要在发送AJAX请求之前访问服务器时间的应用程序,唯一的方法就是能够有一个使用AJAX从服务器请求时间的函数,所以在我的函数中我有以下代码:

$.ajax({
    type:"GET",
    url:"app/time.php", 
        success: function(html){ return html; } });

In itself the code is fine, but as it is, I am unable to gain access to the returned HTML, meaning the data is useless to me. I've tried sending the data to another function, and even back on the same function, and it simply adds complexity that I don't think is needed. I've also tried setting the value of the HTML to another variable and then using, but this doesn't work outside of the function.

本身代码很好,但实际上,我无法访问返回的HTML,这意味着数据对我来说毫无用处。我已经尝试将数据发送到另一个函数,甚至回到相同的函数,它只是增加了我认为不需要的复杂性。我也尝试将HTML的值设置为另一个变量,然后使用,但这在函数之外不起作用。

So I'm asking if there is an easy way to have access to the returned "html" variable from the AJAX request so I can set variables on the page equal to the function?

所以我问是否有一种简单的方法可以从AJAX请求访问返回的“html”变量,所以我可以在页面上设置变量等于函数?

2 个解决方案

#1


1  

Do it like this :

像这样做 :

 function getTime() {

       $.ajax({
        type:"GET",
        url:"app/time.php", 
            success: function(html){ setTime(html); } });
    }

    function setTime(time){
    //do what ever you want with time
    }

when ever you call time getTime() an ajax called will be made and the function setTime() will be called, and will get time through its argument.

当你调用时间getTime()时,将调用一个调用的ajax并调用函数setTime(),并通过它的参数获得时间。

or you can do it like this

或者你可以这样做

function getTime() {

function getTime(){

   $.ajax({
    type:"GET",
    url:"app/time.php", 
        success: function(time){ // do what ever with time ; } });
}

I think this way its easier but if you have a long function then the previous one is cleaner I think.

我认为这种方式更容易,但如果你有一个很长的功能,那么前一个我觉得更干净。

#2


0  

You cannot return a value from there. It's an asynchronous function which is fired when the XHR's request has completed successfully. You need to invoke another callback here:

你不能从那里返回一个值。它是一个异步函数,在XHR请求成功完成时触发。你需要在这里调用另一个回调:

 function getTime(cb) {
      .ajax({
         type:"GET",
         url:"app/time.php", 
         success: function(html){ cb(html); } 
      });
 }

 getTime(function(html) {
    // do something with html
 });

#1


1  

Do it like this :

像这样做 :

 function getTime() {

       $.ajax({
        type:"GET",
        url:"app/time.php", 
            success: function(html){ setTime(html); } });
    }

    function setTime(time){
    //do what ever you want with time
    }

when ever you call time getTime() an ajax called will be made and the function setTime() will be called, and will get time through its argument.

当你调用时间getTime()时,将调用一个调用的ajax并调用函数setTime(),并通过它的参数获得时间。

or you can do it like this

或者你可以这样做

function getTime() {

function getTime(){

   $.ajax({
    type:"GET",
    url:"app/time.php", 
        success: function(time){ // do what ever with time ; } });
}

I think this way its easier but if you have a long function then the previous one is cleaner I think.

我认为这种方式更容易,但如果你有一个很长的功能,那么前一个我觉得更干净。

#2


0  

You cannot return a value from there. It's an asynchronous function which is fired when the XHR's request has completed successfully. You need to invoke another callback here:

你不能从那里返回一个值。它是一个异步函数,在XHR请求成功完成时触发。你需要在这里调用另一个回调:

 function getTime(cb) {
      .ajax({
         type:"GET",
         url:"app/time.php", 
         success: function(html){ cb(html); } 
      });
 }

 getTime(function(html) {
    // do something with html
 });