我的代码如下所示:
web.php
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::post('/xyz','xyz@store');
xyz.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class xyz extends Controller
{
/**
* Store a newly created resource in storage.
*
* @param Request $request
* @return Response
*/
public function store(Request $request) {
$name = $request->name;
$email = $request->email;
$password = $request->password;
return response()
->json(['name' => $name, 'email' => $email,'password' =>$password]);
}
}
在这里,我甚至没有使用表单,否则我可以插入csrf_token。这个API有什么遗漏吗?
答案 0 :(得分:0)
如果您使用的是ajax请求,则必须添加:
headers : {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
请按照以下步骤操作:
将令牌存储在根视图文件顶部的“meta”标记中(layouts / app.blade.php)...
<meta name="csrf-token" content="{{ csrf_token() }}">
确保下载Postman Interceptor(将Postman与浏览器同步?)并在浏览器和邮递员中“打开”。
https://chrome.google.com/webstore/detail/postman-interceptor/aicmkgpgakddgnaphhhpliifpcfhicfo?hl=en
在浏览器的应用中执行某些操作,打开控制台并在头部搜索csrf_token的值......
<meta name="csrf-token" content="cbpj1L7ym6fdPJhl5Fc0mH4MMU71gK1zatutgC3d">
在邮递员中添加标题...
X-CSRF-TOKEN cbpj1L7ym6fdPJhl5Fc0mH4MMU71gK1zatutgC3d