0

我正在尝试提交表单。在提交表单之前,我使用 jquery validate 插件来验证输入。验证后,我将在 jquery 中向控制器中的控制器函数发送一个 ajax 请求。输入的插入是成功的,因为我可以看到在我的数据库中插入和显示的值。但是回调返回错误。我尝试将其中一个字段留空,它实际上返回了一个成功回调。

我的验证脚本如下

  $("#registerHere").validate({
    rules:{
        user_name:"required",
        user_email:{
            required:true,
            email: true
        },
        pwd:{
            required:true,
            minlength: 6
        },
        cpwd:{
            required:true,
            equalTo: "#pwd"
        },
        role:"required"
    },
    messages:{
        user_name:"Enter your first and last name",
        user_email:{
            required:"Enter your email address",
            email:"Enter valid email address"
        },
        pwd:{
            required:"Enter your password",
            minlength:"Password must be minimum 6 characters"
        },
        cpwd:{
            required:"Enter confirm password",
            equalTo:"Password and Confirm Password must match"
        },
        role:"Select Role"
    },
    errorClass: "help-inline",
    errorElement: "span",
    highlight:function(element, errorClass, validClass) {
        $(element).parents('.control-group').addClass('error');
    },
    unhighlight: function(element, errorClass, validClass) {
        $(element).parents('.control-group').removeClass('error');
        $(element).parents('.control-group').addClass('success');
    },
});

我的jquery ajax post方法如下

 $('#register').click(function(){ 
    //alert('register k');

    var user_name = $('#registerHere').find('#user_name').val();

    var user_password = $('#registerHere').find('#pwd').val();

    var user_email = $('#registerHere').find('#user_email').val();

    var user_role = $('#registerHere').find('#role option:selected').val();

    //if(user_name =="" || user_password==""||user_email==""||user_role=="")
        //return;

    var post_data = 
    {
        username:user_name,
        password:user_password,
        email:user_email,
        role:user_role
    };  

    $.ajax({
        type: "POST",
        dataType:"json",
        url:"login_register/register_controller_function",
        data: post_data,
        success: function(data) {
                alert(data);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown)
        {
            alert(textStatus);

        }
    });


});

我的控制器脚本

    public function register_controller_function()
{
    $this->load->database();
    $this->load->model('login_model');

    $user_name=$this->input->post('username');
    $user_password=$this->input->post('password');
    $user_email=$this->input->post('email');
    $user_role=$this->input->post('role');


    $result = $this->login_model->register_model_function($user_name,$user_password,$user_email,$user_role);
    echo json_encode($result);
}

其中 register_model_function 将值插入数据库。

当我使用调试器查看请求时,它略有不同。

如果我将一个有成功回调的字段留空,则请求标头如下所示

Request    URL:http://127.0.0.1/mathplication_v2/login_register/register_controller_function
Request Method:POST
Status Code:200 OK

POST /mathplication_v2/login_register HTTP/1.1

Host: 127.0.0.1

Connection: keep-alive

Content-Length: 72

Cache-Control: max-age=0

Origin: http://127.0.0.1

User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5

Content-Type: application/x-www-form-urlencoded

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Referer: http://127.0.0.1/mathplication_v2/login_register

Accept-Encoding: gzip,deflate,sdch

Accept-Language: en-US,en;q=0.8

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

如果我填写会有错误回调的字段,请求头看起来像这样

Request URL:http://127.0.0.1/mathplication_v2/login_register
Request Method:POST
Status Code:200 OK

POST /mathplication_v2/login_register HTTP/1.1

Host: 127.0.0.1

Connection: keep-alive

Content-Length: 72

Cache-Control: max-age=0

Origin: http://127.0.0.1

User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5

Content-Type: application/x-www-form-urlencoded

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Referer: http://127.0.0.1/mathplication_v2/login_register

Accept-Encoding: gzip,deflate,sdch

Accept-Language: en-US,en;q=0.8

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

请求 url 不同,但在我的 jquery 的 ajax 请求中,我已经指定了控制器 url。

知道有什么问题吗?

4

0 回答 0