![](/img/trans.png)
[英]Why the same POST request works in POSTMAN but not in browser AJAX (404 Not Found)?
[英]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.