M
M
med1um2021-04-14 20:08:57
AJAX
med1um, 2021-04-14 20:08:57

How does XMLHttpRequest pass data?

I accidentally forgot to change the request type from 'GET' to 'POST', but the data was successfully sent to the server!

var xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.setRequestHeader('Content-type', 'application/json');
xhr.send('{"name":"' + name + '","age":"' + age + '"}');


In this regard, two questions:
1) It turns out that it does not matter what to specify - "GET" or "POST" - what is important is just passed to .send() ?
2) If you don't use JSON.stringify() then how should you prepare the json data before sending it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Developer, 2021-04-14
@samodum

There is a difference between a get and a post.
The fact that you got the correct answer is a backend error on the one hand and luck on the other, because you transferred little data. Try getom "send" a hundred megs

P
Pavel Kornilov, 2021-04-15
@KorniloFF

1. Has. Both in terms of the volume of the transmitted request, and in any REST.
2. Can be manually{"a":"b"}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question