Return ajax 调用后的结果
Return a result after ajax call
我一直在尝试使用 ajax 编写一个小函数,但我真的很困惑如何 return 结果。我在这里看到了一些示例,但我没有设法将它们应用到我的代码中...
//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;
};
我希望能够 return theLink
将其存储为变量以执行如下操作...
function Nav() {
var theLink = PageR(nav_newCust);
$.mobile.changePage(theLink);
};
请帮忙!!
你为什么不尝试这样的事情:
//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);
};
你不能以这种方式做到这一点,因为你有 ajax 个非阻塞调用并且 PageR 函数总是 return 未定义。
试试这个:
function PageR(iURL) {
$.ajax({
url: './BSK_'+iURL+'.php',
success: function(data) {
$.mobile.changePage ('./BSK_'+iURL+'.php');
},
error: function(data){
$.mobile.changePage ('./'+iURL+'.php');
},
});
};
我一直在尝试使用 ajax 编写一个小函数,但我真的很困惑如何 return 结果。我在这里看到了一些示例,但我没有设法将它们应用到我的代码中...
//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;
};
我希望能够 return theLink
将其存储为变量以执行如下操作...
function Nav() {
var theLink = PageR(nav_newCust);
$.mobile.changePage(theLink);
};
请帮忙!!
你为什么不尝试这样的事情:
//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);
};
你不能以这种方式做到这一点,因为你有 ajax 个非阻塞调用并且 PageR 函数总是 return 未定义。
试试这个:
function PageR(iURL) {
$.ajax({
url: './BSK_'+iURL+'.php',
success: function(data) {
$.mobile.changePage ('./BSK_'+iURL+'.php');
},
error: function(data){
$.mobile.changePage ('./'+iURL+'.php');
},
});
};