该视图有一个调用javascript函数的链接
@extends('layouts.main')
@section('content')
<table class="table">
<tr>
<th>ID</th>
<th>Nombre</th>
<th>Opción</th>
</tr>
@foreach ($tasks as $task)
<tr>
<td>{{$task->id}}</td>
<td>{{$task->name}}</td>
<td><a href="javascript:void(0)" onclick="eliminar({{$task->id}})" class="btn btn-danger">Eliminar</a></td>
</tr>
@endforeach
</table>
@stop
这是javascript代码
function eliminar(id){
$.ajax({
type: "DELETE",
url: "task/"+id,
success: function (data) {
console.log(data);
},
error: function (data) {
alert('Error:', data);
}
});
}
<?PHP
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Task;
class TaskController extends Controller
{
/**
* display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$tasks = Task::all();
return view('mainview',['tasks' => $tasks]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
return "destroy";
}
}
我是Laravel的新手,但我认为csrf适用于表格.
解决方法:
您应该将csrf_field添加到主刀片:
{{ csrf_field() }}
然后将_token添加到您的请求中:
var _token = $('input[name="_token"]').val();
function eliminar(id){
$.ajax({
type: "DELETE",
url: "task/"+id,
data: { _token : _token },
success: function (data) {
console.log(data);
},
error: function (data) {
alert('Error:', data);
}
});
}
或者您可以在ajaxSetup中添加一次,它将影响对$.ajax或基于Ajax的衍生产品(如$.get())的所有调用:
$.ajaxSetup(
{
headers:
{
'X-CSRF-Token': $('input[name="_token"]').val()
}
});
希望这可以帮助.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。