【问题标题】:AJAX request not passing the data on controller's $_POSTAJAX 请求未在控制器的 $_POST 上传递数据
【发布时间】: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


【解决方案1】:

在控制器的函数中添加这样的测试行:$response["test"] = "test";。 确保它会显示在您的控制台中。

将您的 ajax 网址更改为: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));

希望这会有所帮助。

【讨论】:

  • 这就是我所做的。 $email = $this->security->xss_clean($this->input->post("email")); $password = $this->security->xss_clean($this->input->post("password")); $response['submitted_data'] = $_POST;
  • 您的控制台中是否出现任何内容?
  • 是的,先生。但 $response['submitted_data'] = $_POST 仍然显示 null。
  • 我在我的环境中尝试过你的代码,默认我有 403 Forbidden 错误,但是当我删除 contentType: 'application/json; charset=utf-8', 并定义正确的 url:url: '<?= base_url("index.php/login/ajax_login"); ?>', 它就像一个魅力。试试看。
【解决方案2】:

你的代码是这样的:

var email = $("#email").val();
var password = $("#password").val();

data: { email: $("#email").val(),password: $("#password").val() },

这就是您在控制器中输入emailpassword 的帖子被识别的原因。

data 属性更改为:
data: { 'email': $("#email").val(),'password': $("#password").val() },

如果有帮助,请告诉我。最好的问候

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-28
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多