1

我一直在尝试使用 ajax 编写一个小函数,但我真的很想知道如何返回结果。我在这里看到了一些例子,但我没有设法将它们应用到我的代码中......

//function to detect bespoke page
function PageR(iURL) {
  var theLink;
   $.ajax({
      url: './BSK_'+iURL+'.php', //look to see if bespoke page exists
      success: function(data){
        theLink = ('./BSK_'+iURL+'.php'); //if it does display that page
      },
      error: function(data){
          theLink = ('./'+iURL+'.php'); //if it doesn't display the standard page
      },

    }); //end $.ajax
    return theLink;
};

我希望能够返回theLink以将其存储为变量来执行以下操作...

function Nav() {
  var theLink = PageR(nav_newCust);
  $.mobile.changePage(theLink);
};

请帮忙!!

4

2 回答 2

2

你为什么不尝试这样的事情:

//function to detect bespoke page
function PageR(iURL, callback) {
  var theLink;
   $.ajax({
      url: './BSK_'+iURL+'.php', //look to see if bespoke page exists
      success: function(data){
        theLink = ('./BSK_'+iURL+'.php'); //if it does display that page
      },
      error: function(data){
          theLink = ('./'+iURL+'.php'); //if it doesn't display the standard page
      },
      complete: function(){
         callback(theLink);
      }
    }); //end $.ajax
};

function Nav() {
  PageR(nav_newCust, $.mobile.changePage);
};
于 2015-07-08T17:34:06.037 回答
0

你不能这样做,因为你有非阻塞的 ajax 调用,而且 PageR 函数总是返回未定义的。

尝试这个:

function PageR(iURL) {
   $.ajax({
      url: './BSK_'+iURL+'.php',
      success: function(data) {
        $.mobile.changePage ('./BSK_'+iURL+'.php');
      },
      error: function(data){
          $.mobile.changePage ('./'+iURL+'.php');
      },
    });
};

于 2015-07-08T17:36:05.790 回答