我怎样才能返回从jQuery的Ajax请求的数据? [英] How can I get returning data from jquery ajax request?
问题描述
function isNewUsername(str){
var result;
$.post('/api/isnewusername',
{username:str},
function(data) {
result = data.result;
},
"json");
return result;
}
所以,我的问题很简单,但我不能弄明白。我想从isnewusername功能访问的结果。我对答案很好奇,因为我在上面花了1小时。
谢谢
So , my problem is very simple but I can not figure it out . I want to access result from the isnewusername function . I am very curious about answer because I spent 1 hour on it . Thank you
推荐答案
作为一个快速的注意,当您使用的 jQuery的岗位职能您使用的是 jQuery的AJAX功能的简写形式意味着你正在做一个异步调用。因此,只有在一个成功的反应是jQuery的会打电话给你的函数,并把结果从你的服务器端调用在数据
您的成功回调的参数。
As a quick note when you use the jQuery post function you are using a shorthand form of the jQuery ajax function which means you are doing an asynchronous call. Therefore only on a successful response is jQuery going to call your function and put the result from your server side call in the data
parameter of your success callback.
要说明:
function isNewUsername(str){
$.post('/api/isnewusername',
{username:str},
function(data) {
alert(data.result);
},
"json");
}
这是说你可以改变你的code指定<一个href=\"http://stackoverflow.com/questions/133310/how-can-i-get-jquery-to-perform-a-synchronous-rather-than-asynchronous-ajax-req/133327#133327\">a同步回调的但具有直到返回请求锁定的用户浏览器的潜力。
That said you can change your code to specify a synchronous callback but that has the potential to lock the users browser until the request is returned.
这篇关于我怎样才能返回从jQuery的Ajax请求的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!