jsonstore icon indicating copy to clipboard operation
jsonstore copied to clipboard

XMLHttpRequest POST 400 Bad Request

Open owocean opened this issue 4 years ago • 3 comments

I am using a XML Http Request with POST to try and upload data to your site, but I keep getting 400 Bad request. The content type IS json, and the data IS in json format. Is the site down? Also when I use a GET request i get {result: null, okay: true}. Is that supposed to happen? Im new to this service and I don't know how it works

owocean avatar Sep 20 '19 14:09 owocean

Hey @owocean, can you share an example of the code you're trying to run?

bluzi avatar Sep 21 '19 14:09 bluzi

@bluzi I'm facing the same issue. I'm using browser-based JS. I tried using an ajax request:

$.ajax({
	'url': endpoint,
	'type': 'POST',
	'data': this.someJSON,
	'dataType': 'json',
	'contentType': 'application/json; charset=utf-8'
})

Chrome tells me:

Access to XMLHttpRequest at 'https://www.jsonstore.io/XXXXXXXXXXXXX/t' from origin 'https://XXXXXXXXX.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I then tried fetch, as suggested in the readme's JS section:

fetch(endpoint, {
	headers: {
		'Content-type': 'application/json'
	},
	method: 'POST',
	body: this.someJSON,
});

Now Chrome tells me:

(index):1 Access to fetch at 'https://www.jsonstore.io/XXXXXXXXXXXXX/t' from origin 'https://XXXXXXXXX.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
main.js:142 POST https://www.jsonstore.io/XXXXXXXXXXXXX/t net::ERR_FAILED
Uncaught (in promise) TypeError: Failed to fetch

Then I tried adding no-cors:

fetch(endpoint, {
	headers: {
		'Content-type': 'application/json'
	},
	method: 'POST',
	mode: 'no-cors',
	body: this.someJSON,
});

Chrome tells me:

main.js:142 POST https://www.jsonstore.io/XXXXXXXXXXXXX/t net::ERR_ABORTED 400 (Bad Request)

How do I prevent the CORS issue?

Raymo111 avatar Feb 29 '20 22:02 Raymo111

Edit: Seems like I just didn't do JSON.stringify() for the data. Issue resolved.

Raymo111 avatar Mar 03 '20 03:03 Raymo111