mardi 25 septembre 2018

Laravel & Vue.js API file uploading doesn't work

I have a problem with file uploading on my Laravel&Vue.js website using API I get - 500 Server Error "SyntaxError: Unexpected token < in JSON at position 0" I'm trying to create new value in my database, for this I use pop up form with image uploading and other fields like username, email, phone etc. I've tested my API via Postman - it works fine, but when I try to create this directly on my website - it desn't work

you can check a function which must create new value(startup) in DB:

createStartup() {
            fetch('/api/startup', {
                method: 'POST',
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'multipart/form-data'
                },
                body: JSON.stringify(this.new_startup),
            })
                .then(res => res.json())
                .then(res => {
                    $('#createStartUp').modal('hide');
                    alert('New Startup Created!');
                    // this.fetchStartups();
                })
                .catch(err => console.log(err));
}

I think the issue in Headers (I didn't use any Headers in Postman), when I tried to not use any Headers - it didn't wok too, also O tried to use Content-Type with bypass and unfortunately it didn't work

Also I think it must be helpful - how I get image in vue.js:

HTML:

<input id="upload_create" class="file-upload_input" type="file" @change="onFileSelected" >

JS (Vue.js):

onFileSelected(event) {
    this.new_startup.startup_logo = event.target.files[0];
}

Thanks a lot guys for any ideas and helps!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire