How can i post data to a json file using fetch Api


#1
		fetch('https://exampleapid/ehiosunbishop/oshioke/posts', {
				method: 'POST',
				headers: { "Content-type": "application/json"},
				body: JSON.stringify({title:title, thumbnailUrl:thumbnailUrl})
			}).then(function(response) {
				status = response.status;  // Get the HTTP status code
				
				response_ok = response.ok; // Is response.status in the 200-range?
				debug_log(response_ok);
				return response.json();    // This returns a promise
			}).then(function(responseObject) {
				debug_log(responseObject["title"]);
			}).catch(function(err) {
				debug_log(err);
			});


		}

#2

this returns ‘null’ as response .
someone please help.
and it doesnt add to the datas


#3

You try console.log instead of debug_log?

What does status return?

Try

response.json()
  .then(function(data) {
    if (status == 200) {
    	console.log(JSON.stringify(data));
    } else {
        console.log("not ok");
    }
  })
  .catch(function(error) {
        console.log("error");
        console.log(error);
  });

#4

it still doesnt adds the new datas to my json array


#5

#6
[Viewport]: True

#7

Output the JSON.stringify() of your response.


#8

viewport]: null
i always get null as response and when i check the JSON nothing is added
please help me out


#9

You got a true from the status?

What’s the output of console.log(JSON.stringify(response));?

…else share your code and the JSON that you’re sending via your backend.