English 中文(简体)
正在获取 JSson 中附加到字符串
原标题:appending to string in getJSON

是否有人看到这个代码有问题? 当退出被返回时, 它的价值是“ ” 。 我不知道为什么, 也许它与GETJSON的功能有关,

我正试图将一个 URL 传送到此函数中, 让它在 URL 上执行获得 JSON 的功能, 并将结果( 也是我格式化的) 添加到文本字符串中。 我希望它们都放在一个文本字符串中, 这样我就可以用正则解析字符串。 任何帮助都会受到感谢, 谢谢!

function getSearch(url) {
var out =   ;   // output variable

$.getJSON(url, function(data) {
    for(var i = 0; i < numResults; i++) {
        out +=  <p class="hash_list">  +  <a href="http://www.twitter.com/  + data.results[i].from_user +  ">  + data.results[i].from_user +  </a>:   + data.results[i].text +  </p> ;
    }
});

return out;
}
最佳回答

ajax is asynchronous, the code you have would return before the ajax request has finished. There are a few ways you can restructure your code.

您可以在请求完成后执行回溯功能中通过。

function getSearch(url, callback) {
   $.getJSON(url, function(data) {
     var out =   ;    
     // btw what is numResults ?? 
     for(var i = 0; i < numResults; i++) {
        out +=  <p class="hash_list">  +  <a href="http://www.twitter.com/  + data.results[i].from_user +  ">  + data.results[i].from_user +  </a>:   + data.results[i].text +  </p> ;
     }
     if( typeof callback ===  function  ) callback( out ); 
   });
}

然后你会像这样使用这个函数。

getSearch( test.php , function(out){ 
   // here you can use the out variable 
}); 

或者你可以使用 jquery 延后回一个承诺, 并在解决延后时作为争论的一部分, 传递变量作为参数 。 这里举一个例子 。

function getSearch() 
{    
    var deferred = $.Deferred(); 
    $.getJSON(url, function(data) {
     var out =   ;    
     // btw what is numResults ?? 
     for(var i = 0; i < numResults; i++) {
        out +=  <p class="hash_list">  +  <a href="http://www.twitter.com/  + data.results[i].from_user +  ">  + data.results[i].from_user +  </a>:   + data.results[i].text +  </p> ;
     }
     deferred.resolve( out ); // resolve the request and pass in the out argument.  
   });

    return deferred.promise(); // return a promise 
}


$.when( getSearch() ).then(function( out ){  // the out variable will get passed in
     console.log( out );
});
问题回答

AJAX 是 < 强势 > 同步 < / 强势 > 。

您需要使用回溯函数返回值, 和 getJSON 一样 :

function getSearch(url, callback) {
    $.getJSON(..., function() { 
        ...
        callback(...);
    });
}

$.getJSON 是非同步的。 这意味着在 AJAX 调用完成前返回 out

您无法从 AJAX 调用中返回任何内容, 您需要将所有处理 < code>out 的代码添加到回调 < code>$. getJSON 。

我建议你回调一下你的功能

function getSearch(url, callback) {
    var out =   ;
    $.getJSON(url, function(data) {
        for(var i = 0; i < numResults; i++) {
            out +=  <p class="hash_list">  +  <a href="http://www.twitter.com/  + data.results[i].from_user +  ">  + data.results[i].from_user +  </a>:   + data.results[i].text +  </p> ;
        }
        if(typeof callback ===  function ){
            callback(out); // call your callback with data
        }
    });
}

现在,与其期待 getSearch 的返回值,不如发送回调。

getSearch( http://example.com/myurl , function(data){
    // this function will get called sometime in the future,
    // once $.getJSON is done, "data" will be the "out" from the AJAX call
});




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签