【问题标题】:How would I send form data correctly through this post request on the browser?如何通过浏览器上的这个发布请求正确发送表单数据?
【发布时间】:2020-07-16 01:08:16
【问题描述】:

我需要发布正在上传文件的用户的user_idemail

在 Postman 中,在 body -> form-data 部分 - 我所有的 file 作为 key 和图像作为 value 并且它成功发布(文件路径、id 和电子邮件已发送到数据库)。我不确定如何以不是user_idemail 的表单数据的方式执行此操作,因此我将其附加到表单中,但它不起作用。

但是,在浏览器中 - 我收到 500 错误,上面写着:ErrorException: Trying to get property 'id' of non-object in file

我什至尝试删除 formData.append('user_id', this.state.id);formData.append('email', this.state.email);

我做错了什么?

前端代码:

constructor(props) {
    super(props);

    this.state = {
        selectedFile: null,
        user_id: null,
        email: ''
    };

    this.onFormSubmit = this.onFormSubmit.bind(this);
    this.onChange = this.onChange.bind(this);
    this.fileUpload = this.fileUpload.bind(this);
}

componentDidMount() {
    this.getId();
    this.getEmail();
}

getId() {
    console.log("inside getId()");
    let user_id = Cookies.get("id");
    this.setState({user_id: user_id}, () => console.log(this.state.user_id));
}

getEmail() {
    console.log("inside getEmail");
    let email = Cookies.get("email");
    this.setState({email: email}, () => console.log(this.state.email));
}

onFormSubmit(e) {
    e.preventDefault();
    this.fileUpload(this.state.selectedFile);
}

onChange(e) {
    this.setState({ selectedFile: e.target.files[0] });
}

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

    formData.append('file', file);
    formData.append('user_id', this.state.user_id);
    formData.append('email', this.state.email);

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

render() {
        return (
            <form encType='multipart/form-data' id="login-form" className="form" onSubmit={this.onFormSubmit}>
                <input type="file" name="file" onChange={this.onChange}/>
                <button type="submit">Upload</button>
            </form>
        );
    }

后端控制器代码:

public function store(Request $request){
    $filePath = $request->file('file')->getClientOriginalName();
    $id = $request->user()->id;
    $email = $request->user()->email;

   // dd($id, $email);

    $data= [
        'file_path' => $filePath,
        'user_id' => $id,
        'email' => $email
    ];

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

【问题讨论】:

    标签: javascript php reactjs laravel debugging


    【解决方案1】:

    好吧,ErrorException: Trying to get property 'id' of non-object in file 不是很有帮助,你能看到它在说什么吗?如果它的意思是$id = $request-&gt;user()-&gt;id;,那么id 似乎不是user() 的一部分。也许尝试转储$request-&gt;user()

    如果您发送user_idemail,那么您可以发送$request-&gt;user_id$request-&gt;email。你确定有一个活跃的会话吗?

    【讨论】:

    • iduser() 的一部分。我抛弃了$request-&gt;user(),它出现了。我只需要知道如何在我的前端代码中发布idemail。在 Postman 中,我正在登录然后上传文件并成功发送到数据库。我故意在 Postman 中不包括 idemail,它只需要用户登录即可。
    猜你喜欢
    • 2021-01-29
    • 1970-01-01
    • 2016-01-25
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多