Javascript 正在等待get请求完成
好的,所以我正在做一些事情来说明。我有一个函数,它检查用户名是否存在,如果不存在,则返回false 然后我有另一个函数调用它,但它会自动转到else语句,而不是等待get请求完成。我如何让它等到得到响应Javascript 正在等待get请求完成,javascript,jquery,Javascript,Jquery,好的,所以我正在做一些事情来说明。我有一个函数,它检查用户名是否存在,如果不存在,则返回false 然后我有另一个函数调用它,但它会自动转到else语句,而不是等待get请求完成。我如何让它等到得到响应 var accountPassword = "accToGetTix" function checkUsername(username){ $.get("http://www.roblox.com/UserCheck/DoesUsernameExist?username=" + use
var accountPassword = "accToGetTix"
function checkUsername(username){
$.get("http://www.roblox.com/UserCheck/DoesUsernameExist?username=" + username, function(data){
$( document ).ready(function() {
return data.sucess; // true = taken , false = not taken
});
});
};
function makeNewAccount(accountName){
if (checkUsername(accountName) == false){
signupWindow = window.open("http://www.roblox.com/login/signup.aspx");
signupWindow.$("#SignupUsername").val(accountName)
signupWindow.$("#SignupPassword").val(accountPassword)
signupWindow.$("#SignupPasswordConfirm").val(accountPassword)
signupWindow.$('#birthdayMonthSelect option[value="0"]').prop('selected', true)
signupWindow.$('#birthdayDaySelect option[value="0"]').prop('selected', true)
signupWindow.$('#birthdayYearSelect option[value="25"]').prop('selected', true)
signupWindow.$('.gender-circle').click();
} else {
return true; // true = account taken , false = not taken
}
}
makeNewAccount('asdf205m0');
您可以通过如下更新代码来实现这一点
function checkUsername(username, callback) {
$.get("http://www.roblox.com/UserCheck/DoesUsernameExist?username=" + username, function(data) {
callback(data.sucess); // true = taken , false = not taken
});
};
function makeNewAccount(accountName) {
checkUsername(accountName, function(response) {
if (response == false) {
signupWindow = window.open("http://www.roblox.com/login/signup.aspx");
signupWindow.$("#SignupUsername").val(accountName)
signupWindow.$("#SignupPassword").val(accountPassword)
signupWindow.$("#SignupPasswordConfirm").val(accountPassword)
signupWindow.$('#birthdayMonthSelect option[value="0"]').prop('selected', true)
signupWindow.$('#birthdayDaySelect option[value="0"]').prop('selected', true)
signupWindow.$('#birthdayYearSelect option[value="25"]').prop('selected', true)
signupWindow.$('.gender-circle').click();
} else {
return true; // true = account taken , false = not taken
}
});
}
您不能通过异步调用
返回。您需要使用回调。进行一点重构,您就可以使其正常工作:
function checkUsername(username, callback){
$.get("http://www.roblox.com/UserCheck/DoesUsernameExist?username=" + username, function(data){
if (callback) callback(data.sucess); // true = taken , false = not taken
});
};
function makeNewAccount(accountName){
checkUsername(accountName, function(response) {
if (response === false) {
signupWindow = window.open("http://www.roblox.com/login/signup.aspx");
signupWindow.$("#SignupUsername").val(accountName)
signupWindow.$("#SignupPassword").val(accountPassword)
signupWindow.$("#SignupPasswordConfirm").val(accountPassword)
signupWindow.$('#birthdayMonthSelect option[value="0"]').prop('selected', true)
signupWindow.$('#birthdayDaySelect option[value="0"]').prop('selected', true)
signupWindow.$('#birthdayYearSelect option[value="25"]').prop('selected', true)
signupWindow.$('.gender-circle').click();
} else {
return true; // true = account taken , false = not taken
}
})
}
您必须使用.done函数,如下所示:
var accountPassword = "accToGetTix"
function checkUsername(username){
$.get("http://www.roblox.com/UserCheck/DoesUsernameExist", {
"username": username
}).done(function(data){
if (data.success) {
alert("this username already exists");
} else {
makeNewAccount(username);
}
});
};
function makeNewAccount(accountName){
if (checkUsername(accountName) == false){
signupWindow = window.open("http://www.roblox.com/login/signup.aspx");
signupWindow.$("#SignupUsername").val(accountName)
signupWindow.$("#SignupPassword").val(accountPassword)
signupWindow.$("#SignupPasswordConfirm").val(accountPassword)
signupWindow.$('#birthdayMonthSelect option[value="0"]').prop('selected', true)
signupWindow.$('#birthdayDaySelect option[value="0"]').prop('selected', true)
signupWindow.$('#birthdayYearSelect option[value="25"]').prop('selected', true)
signupWindow.$('.gender-circle').click();
} else {
return true; // true = account taken , false = not taken
}
}
您可以在此处找到更多信息:
我不确定get调用返回的是什么,因此,如果需要,您是否必须修改代码。如果需要,也可以在.done函数中使用console.log(数据):p
编辑:
jQuery中的$.get函数向指定的URL发出一个异步http请求。Assynchronous意味着代码不必通过等待请求完成以传递到下一个代码行而以线性顺序运行
javascript代码可以分为两种类型:同步和非同步。让我给你举个例子
这是一个同步代码:
function IWillRunFirst(){
console.log("first!");
}
IWillRunFirst();
alert("I am the second");
var someElement = $("#justAnElement");
someElement.addClass("foo");
console.log("finish");
//this results:
// "first!"
// "I am the second"
// "finish"
function IWillRunFirstButMyResponseWillGetLate(){
$.get("someUrl", {
"myUrlParameter": "foo"
}).done(function(requestResponse){
console.log("Howdy! Did I got late?");
});
}
IWillRunFirstButMyResponseWillGetLate();
alert("I am the second");
var someElement = $("#justAnElement");
someElement.addClass("foo");
console.log("finished");
此代码将以与编写时完全相同的顺序运行。下面是一个同步代码示例:
function IWillRunFirst(){
console.log("first!");
}
IWillRunFirst();
alert("I am the second");
var someElement = $("#justAnElement");
someElement.addClass("foo");
console.log("finish");
//this results:
// "first!"
// "I am the second"
// "finish"
function IWillRunFirstButMyResponseWillGetLate(){
$.get("someUrl", {
"myUrlParameter": "foo"
}).done(function(requestResponse){
console.log("Howdy! Did I got late?");
});
}
IWillRunFirstButMyResponseWillGetLate();
alert("I am the second");
var someElement = $("#justAnElement");
someElement.addClass("foo");
console.log("finished");
上述代码的结果可能是:
"I am the second"
"finished"
"Howdy! Did I got late?"
记住:Ajax调用总是同步的;) 我认为最干净的方法是通过jquery deffered使用Promissions对象,同时还需要使用jquery$.get方法返回Promissions。
我可以与大家分享代码示例,但了解这个概念会让您受益匪浅。您不能从异步调用返回。。。您需要使用回调。您的条件应该在$.get success callback中,您有一个输入错误:*data.successPlease从回调中取出document.ready()
。这是毫无意义的,令人困惑的。没关系,它成功了。我把success拼错为successto在技术上是正确的:你可以从异步调用中返回
,它只是做不到OP所期望的。