[英]AJAX request not passing the data on controller's $_POST
I have this ajax code. 我有这个ajax代码。 I am getting the values of my textboxes, the problem here is that the data in ajax call can't be posted in my controller.
我正在获取文本框的值,这里的问题是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);
here is what my controller looks like. 这是我的控制器的外观。
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);
}
There is no problem in json_encode. json_encode中没有问题。 But I am not getting the $_POST['email'] and $_POST['password'] that is passed on my ajax request.
但是我没有得到在我的ajax请求中传递的$ _POST ['email']和$ _POST ['password']。 thank you for all the help.
感谢您的所有帮助。
In your Controller's function add a test line like this: $response["test"] = "test";
在您的Controller函数中,添加如下测试行:
$response["test"] = "test";
. 。 Make sure that it will show up in your console.
确保它会显示在您的控制台中。
Change your ajax url to: index.php/login/ajax_login
将您的ajax网址更改为:
index.php/login/ajax_login
In CodeIgniter, it's better to use the $this->input->post();
在CodeIgniter中,最好使用
$this->input->post();
instead of $_POST[]
. 而不是
$_POST[]
。 If you want xss protection (recommended) than this is how you need to declare that: 如果要使用xss保护(推荐),则需要声明以下内容:
$email = $this->security->xss_clean($this->input->post("email"));
$password = $this->security->xss_clean($this->input->post("password"));
And finally, at the end of your Controller set the output type instead of echo
: 最后,在Controller的最后,设置输出类型,而不是
echo
:
$this->output->set_content_type('application/json')->set_output(json_encode($response));
Hope this helps. 希望这可以帮助。
You had your code like this: 您的代码如下:
var email = $("#email").val();
var password = $("#password").val();
data: { email: $("#email").val(),password: $("#password").val() },
that's why your input post for email
and password
in the controller was recognized. 这就是为什么您在控制器中输入
email
和password
输入被识别的原因。
Change data
property into this: 将
data
属性更改为此:
data: { 'email': $("#email").val(),'password': $("#password").val() },
Let me know if it helped. 让我知道是否有帮助。 Best Regards
最好的祝福
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.