【发布时间】:2016-05-04 05:42:47
【问题描述】:
我有这个 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'] 和 $_POST['password'] 。谢谢大家的帮助。
【问题讨论】:
-
'baseurl' 变量未定义。请检查网址是否正确。如果您使用 chrome 转到控制台并检查日志 xmlhttprequests 并查看 ajax 是否正在运行,如果它正在运行,请检查它是否是正确的 url
-
不,baseurl 不是未定义的先生。
-
你检查了控制台中的ajax url吗?
标签: php jquery ajax codeigniter