jquery在codeigniter中使用jquery ajax post验证插件

时间:2012-06-27 08:36:31

标签: jquery ajax codeigniter post

我正在尝试提交表单。在我提交表单之前,我使用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

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

知道出了什么问题吗?

0 个答案:

没有答案