Ajax不断收到错误[关闭]

this is my ajax code and im using Codeigniter framework, and im trying to send data to the controller

<script>
    $('#mssgg').submit(function(){
        var ms = $('#mess').val();
        $.ajax({
            type:'POST',
            url:'http://localhost/index.php/C_Main_page/store_chat',
            data:'messege'+ms,
            error:function(data){
                alert("error");
            },
            success:function(data){
                alert("success");
            }
        }); 
    }); 
</script>

i keep getting error, what did i do wrong??

  1. You need to stop the form submission because it refreshes the page so motive of ajax ends.
  2. You should pass an object although you have a wrong data string to pass data:'messege'+ms,.

This: data:'messege'+ms, either should be data:'messege='+ms, or better one as an object data:{messege:ms},.


<script>
    $('#mssgg').submit(function(e){
        e.preventDefault(); // <----stop the form submission.
        var ms = {messege : $('#mess').val() }; //<----create an object
        $.ajax({
            type:'POST',
            url:'http://localhost/index.php/C_Main_page/store_chat',
            data:ms, //<----and pass that object here.
            error:function(data){
                alert("error");
            },
            success:function(data){
                alert("success");
            }
        }); 
    }); 
</script>