我正在尝试从 ajax 调用中获取输出的值。我得到的是NULL,无论我做什么。我的猜测是我的代码结构错误,希望熟悉ajax的人能发现我的错误:
我的 php.ini 中有以下代码。这段代码有效,我已经测试过了,它打印了预期的输出:
$output_string = array();
foreach ($resultfromdb as $key) {
if (($key) && ($key[0]['username'] !="")) {
//echo "Username is already taken";
$output_string = array('outputmsg' => 'User name is already taken');
} else {
//$success = "User name is already in use";
$output_string = array('outputmsg' => 'User name is free for use');
}
}
echo json_encode($output_string);
这是我的js。文件
// list all variables used here...
var
regform = $('#reg-form'),
memberusername = $('#memberusername'),
memberpassword = $('#memberpassword'),
memberemail = $('#memberemail'),
memberconfirmpassword = $('#memberconfirmpassword');
regform.submit(function(e) {
e.preventDefault();
var memberusername = $(this).find("#memberusername").val();
var memberemail = $(this).find("#memberemail").val();
var memberpassword = $(this).find("#memberpassword").val();
var memberconfirmpassword = $(this).find("#memberconfirmpassword").val();
var url = $(this).attr("action");
console.log(url);
return $.ajax({
type:'POST',
url: $(this).attr("action"),
dataType: 'json',
data: {memberusername: memberusername, memberemail: memberemail, memberpassword: memberpassword},
success: function(result){
// $('#result_table').append(output_string);
alert(result);
alert(output_string);
alert(result.output_string);
console.log(result.output_string);
} // End of success function of ajax form
}); // End of ajax call
});
alert 和 console.log 都打印 null... 而不是变量 $output_string 的值。有什么帮助吗?
提前致谢