Javascript 在函数内执行jquery ajax调用时出现问题
我想在函数中放置一个ajax调用,因为我在多个位置重复使用它。我想要返回的响应的操纵版本。下面是我试图做的(大大简化)Javascript 在函数内执行jquery ajax调用时出现问题,javascript,jquery,ajax,Javascript,Jquery,Ajax,我想在函数中放置一个ajax调用,因为我在多个位置重复使用它。我想要返回的响应的操纵版本。下面是我试图做的(大大简化) a=getAjax(); $('body')。追加(''+a+''); 函数getAjax(){ $.ajax({ 键入:“获取”, url:'someURL', 成功:功能(响应){ 返回响应; }); } 然而,实际情况是,append函数在“a”之前运行已在getAjax函数中定义。有什么想法吗?AJAX是异步的。这意味着成功处理程序中的代码将延迟到请求成功,而其余代码
a=getAjax();
$('body')。追加(''+a+'');
函数getAjax(){
$.ajax({
键入:“获取”,
url:'someURL',
成功:功能(响应){
返回响应;
});
}
然而,实际情况是,append函数在“a”之前运行已在getAjax函数中定义。有什么想法吗?AJAX是异步的。这意味着成功处理程序中的代码将延迟到请求成功,而其余代码将正常继续。您需要将相关代码放入AJAX成功处理程序中:
getAjax();
function getAjax() {
$.ajax({
type: "GET",
url: 'someURL',
success: function(response) {
$(document.body).append('<div>'+response+'</div>');
});
}
现在可以使用回调函数内联执行代码:
getAjax(function(response) {
$(document.body).append('<div>'+response+'</div>');
});
或者别的什么
当AJAX请求完成时,将处理匿名函数调用中的代码。为什么不在成功回调中将响应返回给另一个函数。这将处理您对不同响应的需求:
getAjax();
function getAjax() {
$.ajax({
type: "GET",
url: 'someURL',
success: function(response) {
AppendResponse(response);
});
}
function AppendResponse(response) {
$('body').append('<div>'+response+'</div>');
}
getAjax();
函数getAjax(){
$.ajax({
键入:“获取”,
url:'someURL',
成功:功能(响应){
响应(response);
});
}
函数响应(response){
$('body')。追加(''+response+'');
}
我的一个建议是将触发器传递给要在AJAX函数中运行的命令,以便它在AJAX收到响应后运行-
a = getAjax();
function getAjax() {
$.ajax({
type: "GET",
url: 'someURL',
success: function(response) {
inputText(response);
});
}
inputText(someText) {
$(document.body).append('<div>'+ someText +'</div>');
}
a=getAjax();
函数getAjax(){
$.ajax({
键入:“获取”,
url:'someURL',
成功:功能(响应){
输入文本(应答);
});
}
输入文本(someText){
$(document.body).append(“”+someText+“”);
}
通过这种方式,您可以创建if语句/其他备选方案,以继续对不同的结果使用相同的AJAX命令。有两种方法可以对此进行标记。一种是使用成功回调:
$.ajax({
type: "GET",
url: 'someURL',
success: function(response) {
AppendResponse(response);
});
另一个是将async设置为false:
var-a;
getAjax();
$('body')。追加(''+a+'');
函数getAjax(){
$.ajax({
键入:“获取”,
url:'someURL',
async:false,
成功:功能(响应){
a=响应;
});
}
关于非异步的重要注意事项:
跨域请求和数据类型:“jsonp”请求不支持同步操作。您可以为函数
getAjax()
提供一个处理程序,但是如果用户需要下一个决策的信息,那么为什么不使用async:false
等待呢
function getAjax(handler) {
$.ajax({
type: "GET",
url: 'someURL',
success: function(response) {
handler(response);
});
};
function callGetAjax(response) {
if(response === undefined) {
getAjax(callGetAjax);
} else {
$('body').append('<div>'+response+'</div>');
}
}
函数getAjax(处理程序){
$.ajax({
键入:“获取”,
url:'someURL',
成功:功能(响应){
处理程序(响应);
});
};
函数callGetAjax(响应){
如果(响应===未定义){
getAjax(callGetAjax);
}否则{
$('body')。追加(''+response+'');
}
}
只需编辑您的代码,将成功回调中的“a”替换为“response”,因为“a”没有定义。我可以使用它,但我并不总是希望在成功的ajax响应上执行相同的操作。这就是我希望返回响应的原因。以这种方式返回响应不是ajax的设计目的。我已经更新了ans我们提供了一种方法来设置调用函数时希望发生的事情。FWIW,您可以将其缩短为
成功:inputText
inputText
是对函数的引用。不仅匿名函数可以用作回调!
a = getAjax();
function getAjax() {
$.ajax({
type: "GET",
url: 'someURL',
success: function(response) {
inputText(response);
});
}
inputText(someText) {
$(document.body).append('<div>'+ someText +'</div>');
}
$.ajax({
type: "GET",
url: 'someURL',
success: function(response) {
AppendResponse(response);
});
var a;
getAjax();
$('body').append('<div>'+a+'</div>');
function getAjax() {
$.ajax({
type: "GET",
url: 'someURL',
async: false,
success: function(response) {
a = response;
});
}
function getAjax(handler) {
$.ajax({
type: "GET",
url: 'someURL',
success: function(response) {
handler(response);
});
};
function callGetAjax(response) {
if(response === undefined) {
getAjax(callGetAjax);
} else {
$('body').append('<div>'+response+'</div>');
}
}