<small id='pyXBR'></small><noframes id='pyXBR'>

  • <i id='pyXBR'><tr id='pyXBR'><dt id='pyXBR'><q id='pyXBR'><span id='pyXBR'><b id='pyXBR'><form id='pyXBR'><ins id='pyXBR'></ins><ul id='pyXBR'></ul><sub id='pyXBR'></sub></form><legend id='pyXBR'></legend><bdo id='pyXBR'><pre id='pyXBR'><center id='pyXBR'></center></pre></bdo></b><th id='pyXBR'></th></span></q></dt></tr></i><div id='pyXBR'><tfoot id='pyXBR'></tfoot><dl id='pyXBR'><fieldset id='pyXBR'></fieldset></dl></div>
    • <bdo id='pyXBR'></bdo><ul id='pyXBR'></ul>

      1. <tfoot id='pyXBR'></tfoot>
      2. <legend id='pyXBR'><style id='pyXBR'><dir id='pyXBR'><q id='pyXBR'></q></dir></style></legend>

      3. 从 jquery ajax 调用分配变量返回未定义

        时间:2023-09-04

          <legend id='DXJE9'><style id='DXJE9'><dir id='DXJE9'><q id='DXJE9'></q></dir></style></legend>

          • <small id='DXJE9'></small><noframes id='DXJE9'>

            <i id='DXJE9'><tr id='DXJE9'><dt id='DXJE9'><q id='DXJE9'><span id='DXJE9'><b id='DXJE9'><form id='DXJE9'><ins id='DXJE9'></ins><ul id='DXJE9'></ul><sub id='DXJE9'></sub></form><legend id='DXJE9'></legend><bdo id='DXJE9'><pre id='DXJE9'><center id='DXJE9'></center></pre></bdo></b><th id='DXJE9'></th></span></q></dt></tr></i><div id='DXJE9'><tfoot id='DXJE9'></tfoot><dl id='DXJE9'><fieldset id='DXJE9'></fieldset></dl></div>

                  <tbody id='DXJE9'></tbody>

                  <bdo id='DXJE9'></bdo><ul id='DXJE9'></ul>
                  <tfoot id='DXJE9'></tfoot>
                • 本文介绍了从 jquery ajax 调用分配变量返回未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是 jquery 的新手,我试图在 ajax 调用后为变量赋值,但它返回未定义.我的代码如下:

                  I am new to jquery and I am trying to assign a value to a variable after an ajax call but it returns undefined. My code is below:

                  function prepareDocument() {
                  var a = getAverageRating(1);
                  alert(a);
                  }
                  function getAverageRating(pageId) {
                  $.ajax({
                      url: "../services/rating.ashx?action=getAverageRating&pageId=" + pageId,
                      dataType: "text",
                      type: "GET",
                      data: {},
                      error: function (err) {
                          displayDialogBox("Error", err.toString());
                      },
                      success: function (data) {
                          return data;
                      }
                  });
                  }
                  

                  任何帮助将不胜感激.谢谢,

                  Any help would be appreciated. Thanks,

                  推荐答案

                  对于不习惯使用异步操作的人来说,这是一个很常见的问题.它要求您重新考虑如何构建代码,因为您不能只以正常的顺序样式进行编程.

                  This is a very common problem for people not used to using asynchronous operations. It requires you to rethink how you structure your code because you can't just program in normal sequential style.

                  您不能从异步 ajax 调用的成功处理程序返回值.ajax cll 早就完成并且已经返回.从成功处理程序返回一个值只会进入 ajax 代码的内部,而不是返回到您的代码中.

                  You cannot return a value from the success handler of an asynchronous ajax call. The ajax cll has long since completed and already returned. Returning a value from the success handler just goes into the bowels of the ajax code, not back into your code.

                  相反,您必须在成功处理程序或从成功处理程序调用的函数中使用 ajax 调用的结果.

                  Instead, you must use the results of the ajax call in the success handler or in a function you call from the success handler.

                  在您的具体情况下,您的 getAverageRating() 函数可能需要一个回调函数,并且在检索到评分后,将调用回调函数.它不能返回值,因为它会立即返回,然后在未来的某个时间,ajax 调用完成,并使用实际数据调用 ajax 函数中的成功处理程序.

                  In your specific case, your getAverageRating() function probably needs to take a callback function and when the rating has been retrieved, the callback function will be called. It cannot return the value because it returns immediately and then some time in the future, the ajax call completes and the success handler in the ajax function is called with the actual data.

                  function prepareDocument() {
                      getAverageRating(1, function(data) {
                          alert(data);
                      });
                  }
                  
                  function getAverageRating(pageId, fn) {
                  
                      $.ajax({
                          url: "../services/rating.ashx?action=getAverageRating&pageId=" + pageId,
                          dataType: "text",
                          type: "GET",
                          data: {},
                          error: function (err) {
                              displayDialogBox("Error", err.toString());
                          },
                          success: function (data) {
                              fn(data);
                          }
                      });
                  
                  }
                  

                  这篇关于从 jquery ajax 调用分配变量返回未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:何时使用 return,返回的数据会发生什么? 下一篇:我必须在javascript函数中返回一些东西吗?

                  相关文章

                    <small id='af7tq'></small><noframes id='af7tq'>

                    • <bdo id='af7tq'></bdo><ul id='af7tq'></ul>
                      <legend id='af7tq'><style id='af7tq'><dir id='af7tq'><q id='af7tq'></q></dir></style></legend><tfoot id='af7tq'></tfoot>
                      <i id='af7tq'><tr id='af7tq'><dt id='af7tq'><q id='af7tq'><span id='af7tq'><b id='af7tq'><form id='af7tq'><ins id='af7tq'></ins><ul id='af7tq'></ul><sub id='af7tq'></sub></form><legend id='af7tq'></legend><bdo id='af7tq'><pre id='af7tq'><center id='af7tq'></center></pre></bdo></b><th id='af7tq'></th></span></q></dt></tr></i><div id='af7tq'><tfoot id='af7tq'></tfoot><dl id='af7tq'><fieldset id='af7tq'></fieldset></dl></div>