Answer the question
In order to leave comments, you need to log in
How to pass json to server using Fetch in Vue.cli?
I created a json file on the server, I read data from it normally and fill it in the required fields in the app, but I can’t transfer data to this file.
I develop through localhost: 8080 Vue.cli.
2 questions in this regard:
1. Do I need an Apachi server to transfer and write json data to a file,
or
2. Is it enough to just have an index.php config in a folder on the server?
If option 2, what should it be? (I don’t understand in php)
Below is the code with which I am trying to transfer data to a file, where contacts is a link to the data array from the completed form in the form:
methods: {
saveContact(formData) {
let contacts = this.userData.push (formData);
var requestURL = "./data/user_data.json";
let data = JSON.stringify(contacts);
const fetchOptions = {
credentials: "include",
method: "POST",
headers: { "Content-Type": "application/json", },
body: data,
};
fetch(requestURL, fetchOptions)
.then((response) => response.json())
.catch((error) => {
console.log( "Возникла проблема с вашим fetch запросом: ", error.message );
});
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question