繁体   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