var username = $("#username").val();
if(username){
$.ajax({
url: 'check.php',
data: {data: JSON.stringify(username)},
type: 'POST',
dataType: "json",
success: function (data) {
if(data.result == 1) {
var name = "content"; // SAFE anything in global variable?
}
}
});
} else {
alertify.error( "You forgot to type your username" );
}
if(name) {
// if global variable is set => do something
} else {
// if not => do something else
}
我想检查data.result = 1,这是我能想象的唯一方法,以确定data.result是否返回1.
编辑:
我不想通过警报检查它,因为我以后需要使用这个全局变量,任何人都可以帮我解决我的问题?问候
编辑:
$(document).ready(function(){
$("#submitto").submit(function(e){
e.preventDefault();
var username = $("#username").val();
var name;
if(username){
$.ajax({
url: 'check.php',
data: {data: JSON.stringify(username)},
type: 'POST',
dataType: "json",
success: function (data) {
if(data.result == 1) {
name = "content";
}
}
});
} else {
alertify.error( "You forgot to type your username" );
}
if(name) {
alert("ok")
} else {
alert("not ok")
}
});
});
答案 0 :(得分:1)
在ajax()调用之前声明一个变量,并在成功处理程序中为其赋值:
var result;
var username = $("#username").val();
if(username){
$.ajax({
url: 'check.php',
data: {data: JSON.stringify(username)},
type: 'POST',
dataType: "json",
success: function (data) {
result = data.result;
}
});
} else {
alertify.error( "You forgot to type your username" );
}