最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Better way to pass variable from child to parent function javascript - Stack Overflow

programmeradmin7浏览0评论

For example

function getResult(field) {
    $.ajaxSetup ({cache: false, async: false});
    $.get("api.php?field="+field, function(i) {
        result = i;
    });
    return result;
};

The problem with this is that result bees global. If I do var result = i; then the parent function (getResult) cannot see the variable result.

Is there a clever way to do this?

The code that I have posted works correctly. I have set my AJAX calls to be done synchronously.

For example

function getResult(field) {
    $.ajaxSetup ({cache: false, async: false});
    $.get("api.php?field="+field, function(i) {
        result = i;
    });
    return result;
};

The problem with this is that result bees global. If I do var result = i; then the parent function (getResult) cannot see the variable result.

Is there a clever way to do this?

The code that I have posted works correctly. I have set my AJAX calls to be done synchronously.

Share Improve this question edited Nov 29, 2011 at 14:27 joedborg asked Nov 29, 2011 at 14:07 joedborgjoedborg 18.4k33 gold badges87 silver badges122 bronze badges 10
  • 5 Please look on stackoverflow for this answer. It's asked at least once a day. – locrizak Commented Nov 29, 2011 at 14:08
  • possible duplicate of AJAX- response data not saved to global scope? – locrizak Commented Nov 29, 2011 at 14:10
  • 1 Just declare var result = ""; the first thing you do within getResult. – Christofer Eliasson Commented Nov 29, 2011 at 14:11
  • 2 @ChristoferEliasson That would return undefined since the Ajax-request is asynchronous... – Šime Vidas Commented Nov 29, 2011 at 14:12
  • @ŠimeVidas Very true, wasn't thinking clearly, sry about that. – Christofer Eliasson Commented Nov 29, 2011 at 14:14
 |  Show 5 more ments

4 Answers 4

Reset to default 6
function doIt(arr) {
  var result = [];
  $.each(arr, function (y_u_key_jQuery_y_u_no_fix_api, value) {
    result.push(value);
  });
  return result;
}

Generally what you want to do is create a local variable in the outer function that the inner function manipulates through closure scope

Let's assume that your AJAX call is synchronous. You can try this:

function getResult(field) {
  var result;
  $.get("api.php?field="+field, function(i) {
    result = i;
  });

  return result;
 };

This way, the variable result is no more global.

$.get() is an asynchronous call. Trying to return something from it will not work like you think it will. If you want to do something with the result, you should do it in the success callback.

Here is the documentation on get.

You can't actually return a value from a asynchronous call, rather you have to depend upon the response will be be handled by a callback function to be called on response arrival.

function getResult(field, callback) {
    $.get("api.php?field=" + field, callback);
};

USAGE

getResult("someFieldName", function(data) {
    var result = data;
    // do something with the response from server
});
发布评论

评论列表(0)

  1. 暂无评论