簡體   English   中英

為什么在 Postman 中我的帖子請求可以工作,但即使我也在做同樣的事情,但在瀏覽器中卻不行?

[英]How come in Postman my post request works but not in the browser even though I'm doing the same?

在 Postman 中,我可以使用我的端點成功進行身份驗證(我正在使用 Passport)。 然后我可以打開一個新選項卡並使用不同的端點發布到我的 SQL 數據庫 - 文件名(用戶上傳)、email 和 user_id。

在瀏覽器中,我可以成功進行身份驗證。 但是,當我嘗試上傳文件並發送用戶的不記名令牌(以檢測哪個用戶正在上傳)時,我收到一個 500 錯誤,上面寫着:

ErrorException: Trying to get property 'id' of non-object in file.  

我嘗試在我的發布請求中發送 email 和 id,但我得到了同樣的錯誤。 我究竟做錯了什么?

在此處輸入圖像描述

后端 controller 代碼:

public function store(Request $request) {
    $filePath = $request->file('file')->getClientOriginalName();
    $id = $request->user()->id;
    $email = $request->user()->email;
    
    $data = [
        'file_path' => $filePath,
        'user_id' => $id,
        'email' => $email
    ];

    DB::table('my db.photos')->insert($data);
    echo "Record inserted successfully.<br/>";
}

前端代碼:

fileUpload(file) {
    const formData = new FormData()

    console.log(this.state.access_token);

    formData.append('file', file);
    formData.set('access_token', "Bearer " + this.state.access_token);
    // formData.set('email', this.getEmail());
    // formData.set('user_id', this.getId());

    fetch('http://127.0.0.1:8000/api/auth/myendpont/wall-of-fame', {
        method: 'POST',
        body: formData
    })
        .then(response => console.log(response))
        .catch(error => { console.error(error) });
}

也許像這樣?

formData.append('access_token', "Bearer " + this.state.access_token);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM