Ajax表单数据没有保存在数据库中

问题描述:

我想在Laravel-5.2中使用Ajax将用户输入保存到数据库中。Ajax表单数据没有保存在数据库中

这是我route.php

Route::get('xxxxx/{task_id?}',function($task_id){ 
$task = App\XXXXX::find($task_id); 

return response()->json($task); 
}); 

    Route::put('xxxxx/{task_id?}',function(Request $request,$task_id){ 
     $task = App\XXXXX::find($task_id); 

     $task->Name = $request->Name;// 
     $task->Email = $request->Email; 
     $task->Telephone = $request->Telephone; 

     $task->save(); 

     return response()->json($task); 
    }); 

在我看来,保存按钮作为。

<div class="modal-footer"> 
    <button type="button" class="btn btn-primary" id="btn-save" value="update">Save changes</button> 
     <input type="hidden" id="task_id" name="task_id" value="0"> 
</div> 

我使用this tutorial.创建的js文件。
我得到弹出式菜单,Save按钮不起作用。
这里有什么问题?我是Ajax新手。
在此先感谢。

+0

仔细检查您的应用程序的根文件夹名称。更改var url =“...”;到您的文件夹名称--->从评论 – codemax

+1

@Sachit转到浏览器的开发工具中的网络选项卡,并检查XHR类型。在那里你可以看到你的ajax请求的服务器响应。它会显示您的laravel应用程序中发生的错误 – jaysingkar

+0

替换'$(“#btn-save”)。click('into'$('body')。on('click','#btn-save',function ){' –

这是route.php

路线::匹配([ '得到', '后'], '我/保存数据', '@ myController的保存数据');

这是你的HTML:

保存更改

这是你的Controller文件:MyController.php

公共职能保存数据(请求$要求) {$ 输入= $ request-> all();

try{ 

     // You can now use the Subscribe model without its namespace 
     // as you referenced it by its namespace in a use statement. 
     $subscribe = new Subscribe(); 

     // If you want to use a class that is not referenced in a use 
     // statement then you must reference it by its full namespace. 
     $otherModel = new \App\Models\Other\Namespace\OtherModel(); 

     $otherModel = $input['Name']; 
     $otherModel = $input['Email']; 
     $otherModel = $input['Telephone']; 

     // save 
     $otherModel->save();   

    } 
    catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) 
    { 
     \Log::error($e->getMessage(), $context);    
    } 
    catch (Exception $e){ 
     \Log::error($e->getMessage(), $context); 
    } 

    return response()->json(['status'=>'success', 'message'=>'Completed successfully']); 
} 

这是您的js文件:save.js

功能保存(){ 的getData = { 名称:从中获取eliment “值”,//
电子邮件: “value”,//从获得eliment 电话:“value”// from get eliment };

$.ajax({ 
    type: 'post',   // POST Request 
    url: 'localhost/my/save-data', // localhost/my/save-data   // Url of the Route (in this case user/save not only save) 
    data: getData,   // Serialized Data 

    beforeSend: function (xhr) { 
     // Function needed from Laravel because of the CSRF Middleware 
     var token = $('meta[name="csrf_token"]').attr('content'); 

     if (token) { 
      return xhr.setRequestHeader('X-CSRF-TOKEN', token); 
     } 
    }, 
    success: function (data) { 
     // Successfuly called the Controler 

     // Check if the logic was successful or not 
     if (data.status == 'success') { 
      console.log('alles ok'); 
     } else { 
      console.log(data.msg); 
     } 
    }, 
    error: function (data) { 
     // Error while calling the controller (HTTP Response Code different as 200 OK 
     console.log('Error:', data); 
    } 
}); 

}

+0

我曾尝试过这种方式,但也没有效果。 – Sachith