【发布时间】:2017-01-04 04:46:44
【问题描述】:
我的 Authentication.php(控制器)中有这段代码
public function check_user_name(){
$userName = $this->input->post('UserName');
$people = array();
$people[] = array('UserName' => 'Junjun');
$people[] = array('UserName' => 'Kent');
if (in_array($userName, $people))
{
$result = array('result' => true);
}
else
{
$result = array('result' => false);
}
echo json_encode($result);
}
我得到了我的 ajax 代码
$(document).ready(function() {
/*/
* Declarations
/*/
var host = 'http://' + window.location.host + '<?php echo $this->config->item('base_folder'); ?>';
var txtUserName = $('#text-user-name');
var txtPassword = $('#text-password');
var btnNext = $('#button-next');
var btnLogin = $('#button-login');
var grpUserName = $('#group-username');
var grpPassword = $('#group-password');
/*/
* Bindings
/*/
btnNext.on('click', btnNext_Click);
/*/
* Events
/*/
function btnNext_Click() {
event.preventDefault();
//grpUserName.addClass('hidden');
grpUserName.find('h1').text('Checking....');
var post_data = new FormData();
post_data.append('UserName', txtUserName.val());
$.ajax({
url: 'http://localhost/pms/authentication/check_user_name',
type: 'POST',
dataType: 'json',
contentType: false,
processData: false,
data: post_data, //post_data,
success: function(data) { console.log(data);
if(data.result == true){
grpPassword.removeClass('hidden');
grpUserName.addClass('hidden');
}
else{
grpUserName.find('h1').text('Invalid User Name!');
}
},
error: function(xhr, status, error) {
console.log(error);
}
});
}
});
控制台中的问题是它总是返回 false,我的关联数组有什么问题?请帮忙!
【问题讨论】:
-
尝试
var_export( $_POST )看看它的真实内容是什么。 CI有一个小写的坏习惯。所有错误的意思是,该项目不存在于$_POST -
您的人员数组结构错误。
-
@ArtisticPhoenix 先生,我应该把那个放在哪里?我应该在 $_POST 中放入什么 post 变量?
-
@ArtisticPhoenix 我也认为我的结构是错误的,但我不想使用 $people = array('junjun', 'kent');东西
-
@OJT - 添加了一个答案,应该能说明问题。
标签: php arrays ajax codeigniter