Javascript 在Laravel 5.5中使用AJAX提交表单

Javascript 在Laravel 5.5中使用AJAX提交表单,javascript,php,jquery,ajax,laravel,Javascript,Php,Jquery,Ajax,Laravel,我正在尝试在Laravel5.5中使用ajax提交表单 问题是页面正在刷新,并且没有在数据库中提交数据。我需要在不刷新页面的情况下将数据存储在数据库中。 这是我的密码: 控制器 看法 你的脚本中有一个输入错误或错误 jQuery('#btnSelector').click(function(e){ // An error here - it should be e.preventDefault(); event.preventDefault(); getMessage()

我正在尝试在Laravel5.5中使用ajax提交表单 问题是页面正在刷新,并且没有在数据库中提交数据。我需要在不刷新页面的情况下将数据存储在数据库中。
这是我的密码:

控制器 看法
你的脚本中有一个输入错误或错误

jQuery('#btnSelector').click(function(e){
    // An error here - it should be e.preventDefault();
    event.preventDefault();
    getMessage();
});

在Ajax脚本中添加beforeSend:function(request)之后,我的代码现在可以正常工作了

var getMessage = function(){
var timing_tables_name = $("#timing_tables_name").val();
console.log(timing_tables_name);

$.ajax({
    type:'GET',
    url:'/new_timing_table', //Make sure your URL is correct
    dataType: 'json', //Make sure your returning data type dffine as json
     data: 
     {
         timing_tables_name
     },
    beforeSend: function (request) {
            return request.setRequestHeader('X-CSRF-Token', $("meta[name='csrf- 
     token']").attr('content'));
        },
    success:function(data){
        console.log(data); //Please share cosnole data
        if(data.msg) //Check the data.msg isset?
        {
            $("#msg").html(data.msg); //replace html by data.msg
        }

    }
});
 }
并将控制器编辑为与此一样简单

  public function new_timing_table(Request $request){
    $timing_tables =  new Timing_Table;
    $timing_tables->timing_tables_name = $request->timing_tables_name;
    $timing_tables->save();
     $msg = "This is a simple message.";
     return ['msg'=> $msg];
      }

谢谢大家的帮助

我做得很好,谢谢你们的邀请。:)更严肃的一点是:问候语和问候语是不需要的,相反,它们常常把问题弄得杂乱无章,使阅读变得困难。请保持问题的质量,因为本网站的未来用户可能会搜索类似的问题,您的问题也可能帮助他们找到答案;您正在通过e并期望从事件中得到什么不起作用?我帮不了你,当你只是说它不起作用,然后就不管它了——给我具体的细节。什么不起作用?您会遇到什么错误?谢谢您的帮助,我现在已经解决了问题,因为我已经在下面的重播中添加了它
 Route::post('/new_timing_table','Timing_TableControoler@new_timing_table');
jQuery('#btnSelector').click(function(e){
    // An error here - it should be e.preventDefault();
    event.preventDefault();
    getMessage();
});
var getMessage = function(){
var timing_tables_name = $("#timing_tables_name").val();
console.log(timing_tables_name);

$.ajax({
    type:'GET',
    url:'/new_timing_table', //Make sure your URL is correct
    dataType: 'json', //Make sure your returning data type dffine as json
     data: 
     {
         timing_tables_name
     },
    beforeSend: function (request) {
            return request.setRequestHeader('X-CSRF-Token', $("meta[name='csrf- 
     token']").attr('content'));
        },
    success:function(data){
        console.log(data); //Please share cosnole data
        if(data.msg) //Check the data.msg isset?
        {
            $("#msg").html(data.msg); //replace html by data.msg
        }

    }
});
 }
  public function new_timing_table(Request $request){
    $timing_tables =  new Timing_Table;
    $timing_tables->timing_tables_name = $request->timing_tables_name;
    $timing_tables->save();
     $msg = "This is a simple message.";
     return ['msg'=> $msg];
      }