我的问题是我想在它的功能之外使用发布数据,例如:
$(function() {
var s = 'something ';
$.post('ajax.php', {option: 'test'}, function(data) {
s += data; // data: 'hello world';
});
alert(s); // output: 'something ';
});
我的预期结果是“something hello world”。
我该如何处理?
谢谢
最佳答案
不应该是:
$(function() {
var s = 'something ';
$.post('ajax.php', {option: 'test'}, function(data) {
s += data; // data: 'hello world';
alert(s); // output: 'something ';
});
});
很明显,只有在 ajax 调用完成后(正如 Alex 指出的那样),数据才会在 s
中可用,但在那之后它将是持久的......
关于javascript - 如何从 jQuery.post() 获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6757214/