简体   繁体   English

Ajax Jquery运行一个函数,然后在其中包含ajax并返回成功数据

[英]Ajax Jquery run a function then with ajax inside and return success data

var datatobeuse = SearchTable("user_tbl", "first_name", "fname", "last_name", "lname");

I have this above code right after document ready.Then I have below code after document ready 准备好文件后,我便有了上面的代码。然后准备好文件后,我便有了下面的代码

function SearchTable(tablename, column1, label1, column2, label2) {
    $.ajax({
        type: 'POST',
        url: '..user.php',
        data: {
            tablename: tablename,
            column1: column1,
            label1: label1,
            column2: colum2,
            label2: label2,
        },
        dataType: "json",
        success: function (data) {
//            console.log(JSON.stringify(data))
        },
        error: function (data) {

        }
    }).done(function (data) {
    });
}

How can I use the data in success? 如何成功使用数据? I need to have return value so that I can use var datatobeuse which is right after document ready. 我需要具有返回值,以便可以在文档准备好之后使用var databebeuse。

I tried solution from here 我从这里尝试解决方案

Like this one : 像这个 :

function isSession(selector) {
    $.ajax({
        type: "POST",
        url: '/order.html',
        data: ({ issession : 1, selector: selector }),
        dataType: "html",
        success: function(data) {
                // Call this function on success
            someFunction( data );
            return data;
        },
        error: function() {
            alert('Error occured');
        }
    });
}

function someFunction( data ) {
    // Do something with your data
}

But it is not working 但这不起作用

What you should do, is embrace the asynchronous character of javascript. 您应该做的是拥抱javascript的异步字符。

Asynchronous means it takes some time to execute the function, the rest of the javascript stuff will not wait for it. 异步意味着执行该功能需要花费一些时间,其余的javascript东西将不会等待它。 Ajax is an exellent example of this. Ajax就是一个很好的例子。

The solution is the callback. 解决的办法是回调。 A callback is a function that will be called when the rest of the functionnality is ready. 回调是在其余功能就绪时将调用的函数。

I'll take your example to explain. 我将以您的示例进行解释。

function isSession(selector, onReady) {
  $.ajax({
    type: "POST",
    url: '/order.html',
    data: ({ issession : 1, selector: selector }),
    dataType: "html",
    success: function(data) {
      // all is finished.  Now we can call the callback
      if(typeof onReady == 'function') {
        onReady(data);
      }
    },
    error: function() {
    }
  });
}

function someFunction( data ) {
  // Do something with your data
}

// now use them both
var selector = ".links";
isSession(selector, function(data) {
  someFunction( data );
});

Reconsider why exactly you asked this question. 重新考虑为什么要问这个问题。 You don't need a return value for your function. 您不需要函数的返回值。

This takes some different thinking. 这需要一些不同的思考。 You wanted to know how to set the values to datatobeuse, like this: 您想知道如何将值设置为要使用的数据,如下所示:

var datatobeuse = somefunction();
// now datatobeuse is ready and we can use it.
displayOnScreen(datatobeuse);

Instead, think this way 而是这样想

var datatobeuse;
somefunction(function(data) {
  datatobeuse = data;
  displayOnScreen(datatobeuse);
})

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

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