第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

獲得 200 響應(yīng)但未命中控制器 store() 方法

獲得 200 響應(yīng)但未命中控制器 store() 方法

PHP
森林海 2022-05-27 16:24:05
我正在將刀片文件中的表單切換為使用 axios 發(fā)布的基于 vue 的表單。我收到 200 響應(yīng),但我沒(méi)有在控制器中點(diǎn)擊我的 store() 方法。我用原始刀片形式擊中它。到目前為止,我嘗試過(guò)的事情是手動(dòng)添加 csrf 令牌,將標(biāo)頭更改為內(nèi)容類(lèi)型多部分,將發(fā)布數(shù)據(jù)更改為 JSON 字符串,以及更改句柄提交以觸發(fā)表單。這些似乎都沒(méi)有任何效果。我的刀片文件包含 vue 表單@extends('layouts.app')@section('content')<div class="container">    <create-post></create-post></div>@endsection我在 web.php 中的帖子路線(xiàn)Route::get('/p/create', 'PostsController@create');Route::post('/p', 'PostsController@store');我的postsController 中的store 方法,我設(shè)置了一個(gè)console.log 只是為了看看如果我正在點(diǎn)擊它,它看起來(lái)我不是class PostsController extends Controller{    public function __construct()    {        $this->middleware('auth');    }    public function create()    {        return view('posts/create');    }    public function store()    {        console.log('hit store method');        //VALIDATES DATA        $data = request()->validate([            'caption' => 'required',            'image1' => ['required', 'image',],            'image2' => 'image',            'image3' => 'image',            'image4' => 'image',            'image5' => 'image',        ]);        //CREATE IMAGES ARRAY AND UPLOADS TO S3        $imageArray = [];        $imageCount = 1;        foreach($data as $key => $value){            if (strpos($key, 'image') !== false) {                $imagePath = request('image' . $imageCount)->store('uploads', 's3');                $imageCount ++;                array_push($imageArray, 'https://instagrizzle-development.s3.amazonaws.com/' . $imagePath);            }        }        //Relational method HARDCODED profile        auth()->user()->profiles[0]->posts()->create([            'caption' => $data['caption'],            'image' => json_encode($imageArray),        ]);        return redirect('/profile/' . auth()->user()->profiles[0]->id);    }}添加action='/p' enctype="multipart/form-data" method="post"到表單標(biāo)簽似乎是多余的,但我想我會(huì)嘗試一下。也不確定,但 axios 默認(rèn)會(huì)自動(dòng)添加 csrf 令牌嗎?任何幫助,將不勝感激。提前致謝
查看完整描述

2 回答

?
DIEA

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超2個(gè)贊

看起來(lái)你有錯(cuò)誤的中間件


auth 代替 auth:api 在你的控制器構(gòu)造函數(shù)中


見(jiàn):https ://laravel.com/docs/5.8/api-authentication#protecting-routes


您也可能在您的 axios 調(diào)用中遺漏了一些標(biāo)頭,您可以在


你的瀏覽器,如果他們都在那里。


還要驗(yàn)證您的 DOM 以獲取 csrf-token


然后你可以使用一個(gè)包裝 Axios 對(duì)象來(lái)設(shè)置你的默認(rèn)標(biāo)題。


像這樣。(例如將其保存到 api.js)


import Axios from 'axios'


export default() => {

  return Axios.create({

    headers: {

      'Accept': 'application/json',

      'Content-Type': 'application/json',

      'X-Requested-With': 'XMLHttpRequest',

      'X-CSRF-TOKEN' : document.head.querySelector('meta[name="csrf-token"]').content

    }

  })

}

然后導(dǎo)入 axios 你的 vue 組件..


import Api from './api';

然后你可以像使用 axios 對(duì)象一樣使用它


Api().request()

或者,如果您將通過(guò)所有 axios 調(diào)用使用這種單一的身份驗(yàn)證方式。你只需像 laravel 在他們的引導(dǎo)文件中啟動(dòng)一樣


像這樣:


window.axios = require('axios');

window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';


查看完整回答
反對(duì) 回復(fù) 2022-05-27
?
有只小跳蛙

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超8個(gè)贊

用 return 'ok' 替換它;。console.log() 是 javascript,它在 php 中不起作用 – porloscerros Ψ 11 月 28 日 23:37



查看完整回答
反對(duì) 回復(fù) 2022-05-27
  • 2 回答
  • 0 關(guān)注
  • 119 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)