使用jQuery返回函数中的$ .get数据

时间:2022-12-07 22:16:44

I'm trying to call a function that contains jQuery code. I want this function to return the results of the jQuery statement. It is not working, and I'm trying to figure out why.

我正在尝试调用包含jQuery代码的函数。我希望这个函数返回jQuery语句的结果。它没有用,我正在试图找出原因。

function showGetResult (name) {
    var scriptURL = "somefile.php?name=" + name;
    return $.get(scriptURL, {}, function(data) { return data; });
}

alert (showGetResult("John"));

The alert displays "[object XMLHttpRequest]." However, if I run the jQuery statement by itself, outside of a function, it works fine -> $.get(scriptURL, {}, function(data) { alert(data); })

警报显示“[object XMLHttpRequest]”。但是,如果我在函数之外单独运行jQuery语句,它可以正常工作 - > $ .get(scriptURL,{},function(data){alert(data);})

I'd like to be able to re-use this code by putting it inside of a function that returns the $.get data. What fundamental mistake am I making here?

我希望能够通过将其放在返回$ .get数据的函数内来重用此代码。我在这里犯了什么根本性的错误?

5 个解决方案

#1


91  

You have a few different mistakes. First, $.get doesn't return the return value of the callback function. It returns the XHR object. Second, the get function isn't synchronous, it's asynchronous so showGetResult will likely return before get completes. Third, you can't return something from inside the callback to the outer scope. You can, however, bind a variable in the outer scope and set it in the callback.

你有几个不同的错误。首先,$ .get不返回回调函数的返回值。它返回XHR对象。其次,get函数不是同步的,它是异步的,因此showGetResult可能会在完成之前返回。第三,你不能从回调内部向外部范围返回一些东西。但是,您可以绑定外部作用域中的变量并将其设置在回调中。

To get the functionality that you want, you'll need to use $.ajax and set the async option to false. Then you can define a variable in the outer scope and assign it in the ajax callback, returning this variable from the function.

要获得所需的功能,您需要使用$ .ajax并将async选项设置为false。然后,您可以在外部作用域中定义变量,并在ajax回调中分配它,从函数返回此变量。

function showGetResult( name )
{
     var result = null;
     var scriptUrl = "somefile.php?name=" + name;
     $.ajax({
        url: scriptUrl,
        type: 'get',
        dataType: 'html',
        async: false,
        success: function(data) {
            result = data;
        } 
     });
     return result;
}

You would probably be better served, though, figuring out how to do what you want in the callback function itself rather than changing from asynchronous to synchronous calls.

但是,您可能会更好地服务于,找出如何在回调函数本身中执行您想要的操作,而不是从异步调用更改为同步调用。

#2


9  

The fundamental mistake you are making is that the AJAX call is made asynchronously, so by the time you return, the result is not yet ready. To make this work you could modify your code like this:

你犯的根本错误是AJAX调用是异步进行的,所以当你返回时,结果还没有准备好。要使这项工作,您可以像这样修改您的代码:

$(function() {
    showGetResult('John');
});

function showGetResult (name) {
    $.get('somefile.php', { 
        // Pass the name parameter in the data hash so that it gets properly
        // url encoded instead of concatenating it to the url.
        name: name 
    }, function(data) { 
        alert(data); 
    });
}

#3


6  

Looks like you want synchronous request: How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?

看起来你想要同步请求:我如何让jQuery执行同步而非异步的Ajax请求?

Or, you may want to pass callback to your function:

或者,您可能希望将回调传递给您的函数:

function showGetResult (name, callback) {
  var scriptURL = "somefile.php?name=" + name;
  return $.get(scriptURL, {}, callback);
}

showGetResult("John", function(data){ alert(data); });

#4


2  

The fundamental mistake is the "asynchronous" part of AJAX. Because you don't know how long the server will take to send back a response, AJAX methods never "block" -- that is, you don't call out to the server and just sit there waiting for the result. Instead, you go on to something else, but you set up a method, called the "callback", that will fire when the results come back. This method is responsible for doing whatever needs to be done with the data (e.g. injecting it into the page).

根本的错误是AJAX的“异步”部分。因为您不知道服务器发回响应所需的时间,所以AJAX方法永远不会“阻塞” - 也就是说,您不会呼叫服务器而只是坐在那里等待结果。相反,你继续做其他事情,但是你设置了一个叫做“回调”的方法,当结果回来时它会触发。该方法负责执行需要对数据执行的任何操作(例如,将其注入页面)。

#5


1  

This is the wrong way to do. The function(data) is a call back function so whenever return $.get will execute .. there is possibility that call back function would have not been called.

这是错误的做法。函数(data)是一个回调函数,所以只要返回$ .get将执行..有可能没有调用回调函数。

Better you call your post data get function from function(data) method.

最好从函数(数据)方法调用post数据获取函数。

#1


91  

You have a few different mistakes. First, $.get doesn't return the return value of the callback function. It returns the XHR object. Second, the get function isn't synchronous, it's asynchronous so showGetResult will likely return before get completes. Third, you can't return something from inside the callback to the outer scope. You can, however, bind a variable in the outer scope and set it in the callback.

你有几个不同的错误。首先,$ .get不返回回调函数的返回值。它返回XHR对象。其次,get函数不是同步的,它是异步的,因此showGetResult可能会在完成之前返回。第三,你不能从回调内部向外部范围返回一些东西。但是,您可以绑定外部作用域中的变量并将其设置在回调中。

To get the functionality that you want, you'll need to use $.ajax and set the async option to false. Then you can define a variable in the outer scope and assign it in the ajax callback, returning this variable from the function.

要获得所需的功能,您需要使用$ .ajax并将async选项设置为false。然后,您可以在外部作用域中定义变量,并在ajax回调中分配它,从函数返回此变量。

function showGetResult( name )
{
     var result = null;
     var scriptUrl = "somefile.php?name=" + name;
     $.ajax({
        url: scriptUrl,
        type: 'get',
        dataType: 'html',
        async: false,
        success: function(data) {
            result = data;
        } 
     });
     return result;
}

You would probably be better served, though, figuring out how to do what you want in the callback function itself rather than changing from asynchronous to synchronous calls.

但是,您可能会更好地服务于,找出如何在回调函数本身中执行您想要的操作,而不是从异步调用更改为同步调用。

#2


9  

The fundamental mistake you are making is that the AJAX call is made asynchronously, so by the time you return, the result is not yet ready. To make this work you could modify your code like this:

你犯的根本错误是AJAX调用是异步进行的,所以当你返回时,结果还没有准备好。要使这项工作,您可以像这样修改您的代码:

$(function() {
    showGetResult('John');
});

function showGetResult (name) {
    $.get('somefile.php', { 
        // Pass the name parameter in the data hash so that it gets properly
        // url encoded instead of concatenating it to the url.
        name: name 
    }, function(data) { 
        alert(data); 
    });
}

#3


6  

Looks like you want synchronous request: How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?

看起来你想要同步请求:我如何让jQuery执行同步而非异步的Ajax请求?

Or, you may want to pass callback to your function:

或者,您可能希望将回调传递给您的函数:

function showGetResult (name, callback) {
  var scriptURL = "somefile.php?name=" + name;
  return $.get(scriptURL, {}, callback);
}

showGetResult("John", function(data){ alert(data); });

#4


2  

The fundamental mistake is the "asynchronous" part of AJAX. Because you don't know how long the server will take to send back a response, AJAX methods never "block" -- that is, you don't call out to the server and just sit there waiting for the result. Instead, you go on to something else, but you set up a method, called the "callback", that will fire when the results come back. This method is responsible for doing whatever needs to be done with the data (e.g. injecting it into the page).

根本的错误是AJAX的“异步”部分。因为您不知道服务器发回响应所需的时间,所以AJAX方法永远不会“阻塞” - 也就是说,您不会呼叫服务器而只是坐在那里等待结果。相反,你继续做其他事情,但是你设置了一个叫做“回调”的方法,当结果回来时它会触发。该方法负责执行需要对数据执行的任何操作(例如,将其注入页面)。

#5


1  

This is the wrong way to do. The function(data) is a call back function so whenever return $.get will execute .. there is possibility that call back function would have not been called.

这是错误的做法。函数(data)是一个回调函数,所以只要返回$ .get将执行..有可能没有调用回调函数。

Better you call your post data get function from function(data) method.

最好从函数(数据)方法调用post数据获取函数。