如何使用YAHOO.util.Connect.asyncRequest并返回结果?

发布于 2024-09-07 16:01:25 字数 996 浏览 2 评论 0原文

我正在使用 YAHOO.util.Connect.asyncRequest 从数据库获取数据,这里是代码:

function getCountArticle(contentCurValue) {

  var handleSuccess = function (res) {
      var countPubmed = YAHOO.lang.JSON.parse(res.responseText);
      var contentCountPubmed = countPubmed.totalArticleRecords;
      alert(contentCountPubmed); //return 15 for example
  };

  var handleFailure = function () {
      alert("Error connecting data : Bad pubmed query");
  };

  var callback =
  {
    success:handleSuccess,
    failure:handleFailure,
    timeout: 5000
  };

  var sURL = 'qct-list-article.html?term=' + contentCurValue + '&retstart=0' + '&retmax=1';

  var request = YAHOO.util.Connect.asyncRequest('GET',sURL,callback);

}

我希望这个函数返回:“contentCurValue”(例如:15),但是当我尝试使用此代码时,我得到“未定义” " :

var test = getCountArticle();
alert(test); // return undefined, should return 15

我的错误可能是由于异步查询造成的,但是如何强制“var test = getCountArticle();” 等待结果

I'm using YAHOO.util.Connect.asyncRequest to get data from database, here is the code :

function getCountArticle(contentCurValue) {

  var handleSuccess = function (res) {
      var countPubmed = YAHOO.lang.JSON.parse(res.responseText);
      var contentCountPubmed = countPubmed.totalArticleRecords;
      alert(contentCountPubmed); //return 15 for example
  };

  var handleFailure = function () {
      alert("Error connecting data : Bad pubmed query");
  };

  var callback =
  {
    success:handleSuccess,
    failure:handleFailure,
    timeout: 5000
  };

  var sURL = 'qct-list-article.html?term=' + contentCurValue + '&retstart=0' + '&retmax=1';

  var request = YAHOO.util.Connect.asyncRequest('GET',sURL,callback);

}

I would like this function return : "contentCurValue" (eg:15), but when I try to use this code I get "undefined" :

var test = getCountArticle();
alert(test); // return undefined, should return 15

My error is probably due to asynchronous query, but how can I force "var test = getCountArticle();" to wait for results ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许一世地老天荒 2024-09-14 16:01:26

由于调用本质上是异步的,而不是尝试等待响应,因此最好指定一个回调函数来使用数据执行。您可以这样修改您的方法:

 function getCountArticle(contentCurValue, callback) {
  var handleSuccess = function (res) {
      var countPubmed = YAHOO.lang.JSON.parse(res.responseText);
      var contentCountPubmed = countPubmed.totalArticleRecords;
      callback(contentCountPubmed); //return 15 for example
  };
  // ...
} 

那么您的调用代码将是:

 getCountArticle("contentCurValue", function(test) {
    alert(test);
}); 

使用 AJAX 查询返回的值进行的任何进一步执行都将在回调方法内进行。

这个SO帖子本质上是相同的问题,但不是YUI特定的: Getting undefined in javascript调用ajax时

Since the call is by nature asynchronous, rather than try to wait for the response, you would be better off specifying a callback function to execute with the data. You could modify your method like this:

 function getCountArticle(contentCurValue, callback) {
  var handleSuccess = function (res) {
      var countPubmed = YAHOO.lang.JSON.parse(res.responseText);
      var contentCountPubmed = countPubmed.totalArticleRecords;
      callback(contentCountPubmed); //return 15 for example
  };
  // ...
} 

then your calling code would be:

 getCountArticle("contentCurValue", function(test) {
    alert(test);
}); 

Any further execution using the value returned from your AJAX query would proceed inside of your callback method.

This SO post is essentially the same problem, but not YUI specific: Getting undefined in javascript when calling ajax

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文