Javascript jQuery ajax 返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4982983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery ajax return value
提问by Jorge
How can I return the value "pinNumber" from jquery ajax so I can append it outside of the ajax. Here is my code
如何从 jquery ajax返回值“ pinNumber”,以便我可以将它附加到 ajax 之外。这是我的代码
var x = pinLast + 1; for(i=x;i<=pinMany;i++) { var i = x++; var cardNumber = i.toPrecision(8).split('.').reverse().join(''); var pinNumber = ''; jQuery.ajax({ type: "POST", url: "data.php", data: "request_type=generator", async: false, success: function(msg){ var pinNumber = msg; return pinNumber; //pin number should return } }); jQuery('.pin_generated_table').append(cardNumber+' = '+pinNumber+'
'); // the variable pinNumber should be able to go here }
Please ask me if you don't understand.. ^^ thanks
不明白的请追问。^^谢谢
采纳答案by meagar
AJAX is asynchronous by default, you cannot return a value from the callback without making a synchronous call, which you almost certainly don't want to do.
默认情况下,AJAX 是异步的,您不能在不进行同步调用的情况下从回调中返回值,而您几乎肯定不想这样做。
You should supply a real callback function to the success:
handler, and put your program logic there.
您应该为success:
处理程序提供一个真正的回调函数,并将您的程序逻辑放在那里。
回答by Amjad Masad
var pinNumber = $.ajax({
type: "POST",
url: "data.php",
data: "request_type=generator",
async: false
}).responseText;
jQuery('.pin_generated_table').append(cardNumber+' = '+pinNumber+' ');
回答by octopi
It has to do with variable scope. The local variable pinNumber
you create is not accessible outside its wrapping function.
它与变量范围有关。pinNumber
您创建的局部变量在其包装函数之外不可访问。
Perhaps declare pinNumber
globally or if it'll do the trick, simply stick your .append()
inside your success function.
也许pinNumber
全局声明,或者如果它可以奏效,只需将.append()
您的成功函数放在内部即可。
回答by kayz1
var _successEvent = function(response){
$('.pin_generated_table').append(cardNumber + ' = ' + response);
};
$.ajax({
type: "POST",
url: "data.php",
data: "request_type=generator"
}).done(_successEvent);
回答by Grigoriy
You can use this example:
你可以使用这个例子:
window.variable = 'some data';
window.variable = 'some data';
This make you variable global and you can access to this from anywhere
这使您成为全局变量,您可以从任何地方访问它
回答by user2779316
Here is the simple solution.
这是简单的解决方案。
//---------------------------Ajax class ------------------------------//
function AjaxUtil()
{
this.postAjax = function(Data,url,callback)
{
$.ajax({
url: url,
async: true, //must be syncronous request! or not return ajax results
type: 'POST',
data: Data,
dataType: 'json',
success: function(json)
{
callback(json);
}
});
}//--end postAjax
}
//--------------------------------End class--------------------//
var ajaxutil = new AjaxUtil();
function callback(response)
{
alert(response); //response data from ajax call
}
var data={};
data.yourdata = 'anydata';
var url = 'data.php';
ajaxutil.postAJax(data,url,callback);