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

jquery - How to return json object in javascript function? - Stack Overflow

programmeradmin3浏览0评论

I've this function

function getTags(level){
    $.getJSON("php/get-tags.php", { "parent": level }, function(json) {
        return json;
    });
}

I'm calling this function as

$(function(){
    var tags = getTags('0');  
});

The problems is, in the function getTags() the return json is like

{"tags":["Mathematics","Science","Arts","Engineering","Law","Design"]}

but at var tags = getTags('0'), catching the return value, it gets an undefined value.

Is the way I'm returning the value incorrect?

I've this function

function getTags(level){
    $.getJSON("php/get-tags.php", { "parent": level }, function(json) {
        return json;
    });
}

I'm calling this function as

$(function(){
    var tags = getTags('0');  
});

The problems is, in the function getTags() the return json is like

{"tags":["Mathematics","Science","Arts","Engineering","Law","Design"]}

but at var tags = getTags('0'), catching the return value, it gets an undefined value.

Is the way I'm returning the value incorrect?

Share Improve this question edited Jun 11, 2014 at 4:47 Peter Turner 11.4k10 gold badges70 silver badges113 bronze badges asked Jan 11, 2012 at 21:07 ptamzzptamzz 9,35531 gold badges94 silver badges151 bronze badges 1
  • 1 possible duplicate of JavaScript asynchronous return value / assignment with jQuery – Felix Kling Commented Jan 11, 2012 at 21:10
Add a comment  | 

5 Answers 5

Reset to default 8

Like many others already correctly described, the ajax request runs asynchronous by default. So you need to deal with it in a proper way. What you can do, is to return the jXHR object which is also composed with jQuery promise maker. That could looke like

function getTags(level){
    return $.getJSON("php/get-tags.php", { "parent": level });
}

and then deal with it like

$(function(){
    getTags('0').done(function(json) {
        // do something with json
    });
});

getJSON is asynchronous, the function that called it will have finished by the time the HTTP response gets back and triggers the callback.

You cannot return from it.

Use the callback to do whatever you want to do with the data.

You are trying to call an asynchronous function in a synchronous fashion.

When you call getTags, the it triggers a call to your PHP page, if javascript was blocking, then your page would hang until your server responded with the JSON. You need to re-think your logic and trigger a callback.

function getTags(level){
    $.getJSON("php/get-tags.php", { "parent": level }, function(json) {
        //do something with the JSON here.
    });
}

You cannot return from an AJAX call. It's asynchronous. You need to have all logic dealing with the returned data in the callback.

If you need it to work like that, your getTagsfunction must return a value. It does not at the moment. You could achieve this by using $.ajax and setting async to false.

发布评论

评论列表(0)

  1. 暂无评论