我有这个ajax代码。我正在获取文本框的值,这里的问题是ajax调用中的数据无法发布在我的控制器中。
setTimeout(function()
{
var random_pct = 25 + Math.round(Math.random() * 30);
// The form data are subbmitted, we can forward the progress to 70%
neonLogin.setPercentage(40 + random_pct);
var email = $("#email").val();
var password = $("#password").val();
// Send data to the server
$.ajax({
url: baseurl + 'index.php?login/ajax_login',
type: "POST",
dataType: "json",
contentType: 'application/json; charset=utf-8',
data: { email: $("#email").val(),password: $("#password").val() },
error: function(data, status, textError)
{
// alert('Error: ' + xhr.responseText + ' - ' + status + ' - ' + textError);
console.log(data);
console.log('Error: ' + data.responseText + ' - ' + status + ' - ' + textError);
},
success: function(response)
{
console.log($('#email').val());
console.log($('#password').val());
console.log(response);
// alert(response.submitted_data);
// Login status [success|invalid]
var login_status = response.login_status;
// alert(response.login_status);
// Form is fully completed, we update the percentage
neonLogin.setPercentage(100);
// We will give some time for the animation to finish, then execute the following procedures
setTimeout(function()
{
// If login is invalid, we store the
if(login_status == 'invalid')
{
$(".login-page").removeClass('logging-in');
neonLogin.resetProgressBar(true);
}
else
if(login_status == 'success')
{
// Redirect to login page
setTimeout(function()
{
var redirect_url = baseurl;
if(response.redirect_url && response.redirect_url.length)
{
redirect_url = response.redirect_url;
}
window.location.href = redirect_url;
}, 400);
}
}, 1000);
}
});
}, 650);
这是我的控制器的样子。
function ajax_login()
{
// $this->output->set_header('Content-Type: application/json');
$response = array();
//Recieving post input of email, password from ajax request
$email = $_POST["email"];
$password = $_POST["password"];
$response['submitted_data'] = $_POST;
// $response['email'] = $_POST['email'];
// $response['password'] = $_POST['password'];
//Validating login
$login_status = $this->validate_login( $email , $password );
$response['login_status'] = $login_status;
if ($login_status == 'success') {
$response['redirect_url'] = base_url().'index.php?admin/dashboard';
}
//Replying ajax request with validation response
echo json_encode($response);
}
json_encode没有问题。但是我没有得到在我的ajax请求中传递的$_POST['email']和$_POSD['password']。谢谢你的帮助。
在控制器的函数中添加一个测试行,如下所示:$response["test"] = "test";
。确保它会显示在您的控制台中。
将您的ajax url更改为:index.php/login/ajax_login
在CodeIgniter中,最好使用$this->input->post();
而不是$_POST[]
。如果你想要xss保护(推荐),这就是你需要声明的方式:
$email = $this->security->xss_clean($this->input->post("email"));
$password = $this->security->xss_clean($this->input->post("password"));
最后,在控制器的末尾设置输出类型,而不是echo
:
$this->output->set_content_type('application/json')->set_output(json_encode($response));
希望这能有所帮助。
您的代码是这样的:
var email = $("#email").val();
var password = $("#password").val();
data: { email: $("#email").val(),password: $("#password").val() },
这就是为什么您在控制器中为email
和password
输入的帖子被识别的原因。
将data
属性更改为:data: { 'email': $("#email").val(),'password': $("#password").val() },
如果有帮助,请告诉我。向致以最诚挚的问候