Skip to content
This repository has been archived by the owner on Jul 16, 2020. It is now read-only.

XMLHttpRequest POST 400 Bad Request #60

Open
owocean opened this issue Sep 20, 2019 · 3 comments
Open

XMLHttpRequest POST 400 Bad Request #60

owocean opened this issue Sep 20, 2019 · 3 comments

Comments

@owocean
Copy link

owocean commented Sep 20, 2019

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

@bluzi
Copy link
Owner

bluzi commented Sep 21, 2019

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

@Raymo111
Copy link

@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
Copy link

Raymo111 commented Mar 3, 2020

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants