Fetch() API not updating JSON file
up vote
-1
down vote
favorite
I have a JSON file I want to read from and write to that looks like this:
[
"test@example.com"
]
I want to add info to this file using the fetch()
API. So far, I can only read from this file.
let handle = JSON.stringify(`test@example2.com`); // handle is fine irrelevant of "" or ``
const url = `../json/emails.json`; // URL is fine
const options = {
method: `POST`, // PUT return error at second fetch: SyntaxError: "JSON.parse: unexpected character at line 1 column 1 of the JSON data"
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: handle
};
fetch(url) // this fetch is fine
.then(response => {
if (!response.ok) {
throw new Error(`Error getting the stuff`);
}
return response;
})
.then(r => r.json())
.then(data => {
console.log(`Data: ` + data);
});
fetch(url, options)
.then(response => {
if (!response.ok) {
throw new Error(`Error getting the stuff`);
}
return response;
})
.then(a => a.json())
.then(append => {
console.log(`Data updated: ` + append);
}).catch(err => {
console.error('Request failed: ', err);
});
I get no errors (aside from that PUT
comment); ESLint and TSLint don't have any problem with the JS file nor with the JSON file. What am I doing wrong?
javascript fetch-api
New contributor
add a comment |
up vote
-1
down vote
favorite
I have a JSON file I want to read from and write to that looks like this:
[
"test@example.com"
]
I want to add info to this file using the fetch()
API. So far, I can only read from this file.
let handle = JSON.stringify(`test@example2.com`); // handle is fine irrelevant of "" or ``
const url = `../json/emails.json`; // URL is fine
const options = {
method: `POST`, // PUT return error at second fetch: SyntaxError: "JSON.parse: unexpected character at line 1 column 1 of the JSON data"
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: handle
};
fetch(url) // this fetch is fine
.then(response => {
if (!response.ok) {
throw new Error(`Error getting the stuff`);
}
return response;
})
.then(r => r.json())
.then(data => {
console.log(`Data: ` + data);
});
fetch(url, options)
.then(response => {
if (!response.ok) {
throw new Error(`Error getting the stuff`);
}
return response;
})
.then(a => a.json())
.then(append => {
console.log(`Data updated: ` + append);
}).catch(err => {
console.error('Request failed: ', err);
});
I get no errors (aside from that PUT
comment); ESLint and TSLint don't have any problem with the JS file nor with the JSON file. What am I doing wrong?
javascript fetch-api
New contributor
just to clarify - are you trying to update local file with the fetch API?
– rufus1530
Nov 8 at 10:35
Yes. From what @Quentin says, that isn't possible. I'm thinking of translating the above code into Python or using Node.js.
– wombat trash
Nov 8 at 10:37
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have a JSON file I want to read from and write to that looks like this:
[
"test@example.com"
]
I want to add info to this file using the fetch()
API. So far, I can only read from this file.
let handle = JSON.stringify(`test@example2.com`); // handle is fine irrelevant of "" or ``
const url = `../json/emails.json`; // URL is fine
const options = {
method: `POST`, // PUT return error at second fetch: SyntaxError: "JSON.parse: unexpected character at line 1 column 1 of the JSON data"
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: handle
};
fetch(url) // this fetch is fine
.then(response => {
if (!response.ok) {
throw new Error(`Error getting the stuff`);
}
return response;
})
.then(r => r.json())
.then(data => {
console.log(`Data: ` + data);
});
fetch(url, options)
.then(response => {
if (!response.ok) {
throw new Error(`Error getting the stuff`);
}
return response;
})
.then(a => a.json())
.then(append => {
console.log(`Data updated: ` + append);
}).catch(err => {
console.error('Request failed: ', err);
});
I get no errors (aside from that PUT
comment); ESLint and TSLint don't have any problem with the JS file nor with the JSON file. What am I doing wrong?
javascript fetch-api
New contributor
I have a JSON file I want to read from and write to that looks like this:
[
"test@example.com"
]
I want to add info to this file using the fetch()
API. So far, I can only read from this file.
let handle = JSON.stringify(`test@example2.com`); // handle is fine irrelevant of "" or ``
const url = `../json/emails.json`; // URL is fine
const options = {
method: `POST`, // PUT return error at second fetch: SyntaxError: "JSON.parse: unexpected character at line 1 column 1 of the JSON data"
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: handle
};
fetch(url) // this fetch is fine
.then(response => {
if (!response.ok) {
throw new Error(`Error getting the stuff`);
}
return response;
})
.then(r => r.json())
.then(data => {
console.log(`Data: ` + data);
});
fetch(url, options)
.then(response => {
if (!response.ok) {
throw new Error(`Error getting the stuff`);
}
return response;
})
.then(a => a.json())
.then(append => {
console.log(`Data updated: ` + append);
}).catch(err => {
console.error('Request failed: ', err);
});
I get no errors (aside from that PUT
comment); ESLint and TSLint don't have any problem with the JS file nor with the JSON file. What am I doing wrong?
javascript fetch-api
javascript fetch-api
New contributor
New contributor
New contributor
asked Nov 8 at 10:28
wombat trash
104
104
New contributor
New contributor
just to clarify - are you trying to update local file with the fetch API?
– rufus1530
Nov 8 at 10:35
Yes. From what @Quentin says, that isn't possible. I'm thinking of translating the above code into Python or using Node.js.
– wombat trash
Nov 8 at 10:37
add a comment |
just to clarify - are you trying to update local file with the fetch API?
– rufus1530
Nov 8 at 10:35
Yes. From what @Quentin says, that isn't possible. I'm thinking of translating the above code into Python or using Node.js.
– wombat trash
Nov 8 at 10:37
just to clarify - are you trying to update local file with the fetch API?
– rufus1530
Nov 8 at 10:35
just to clarify - are you trying to update local file with the fetch API?
– rufus1530
Nov 8 at 10:35
Yes. From what @Quentin says, that isn't possible. I'm thinking of translating the above code into Python or using Node.js.
– wombat trash
Nov 8 at 10:37
Yes. From what @Quentin says, that isn't possible. I'm thinking of translating the above code into Python or using Node.js.
– wombat trash
Nov 8 at 10:37
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
fetch()
is an API for making HTTP requests.
It can't write to files. In particular, nothing can write to arbitrary URLs. (Imagine if it was possible for any browser to write new data to http://www.google.com/
!)
If you want your PUT or POST request to change data on your server, then you must write server-side code to process the request and edit the file.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
fetch()
is an API for making HTTP requests.
It can't write to files. In particular, nothing can write to arbitrary URLs. (Imagine if it was possible for any browser to write new data to http://www.google.com/
!)
If you want your PUT or POST request to change data on your server, then you must write server-side code to process the request and edit the file.
add a comment |
up vote
3
down vote
accepted
fetch()
is an API for making HTTP requests.
It can't write to files. In particular, nothing can write to arbitrary URLs. (Imagine if it was possible for any browser to write new data to http://www.google.com/
!)
If you want your PUT or POST request to change data on your server, then you must write server-side code to process the request and edit the file.
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
fetch()
is an API for making HTTP requests.
It can't write to files. In particular, nothing can write to arbitrary URLs. (Imagine if it was possible for any browser to write new data to http://www.google.com/
!)
If you want your PUT or POST request to change data on your server, then you must write server-side code to process the request and edit the file.
fetch()
is an API for making HTTP requests.
It can't write to files. In particular, nothing can write to arbitrary URLs. (Imagine if it was possible for any browser to write new data to http://www.google.com/
!)
If you want your PUT or POST request to change data on your server, then you must write server-side code to process the request and edit the file.
answered Nov 8 at 10:32
Quentin
631k718501018
631k718501018
add a comment |
add a comment |
wombat trash is a new contributor. Be nice, and check out our Code of Conduct.
wombat trash is a new contributor. Be nice, and check out our Code of Conduct.
wombat trash is a new contributor. Be nice, and check out our Code of Conduct.
wombat trash is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205841%2ffetch-api-not-updating-json-file%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
just to clarify - are you trying to update local file with the fetch API?
– rufus1530
Nov 8 at 10:35
Yes. From what @Quentin says, that isn't possible. I'm thinking of translating the above code into Python or using Node.js.
– wombat trash
Nov 8 at 10:37