Why do we pass null to XMLHttpRequest.send?

前端 未结 4 1556
温柔的废话
温柔的废话 2020-12-15 03:41

Why is send so often called as

xhr.send(null)

instead of

xhr.send()

?

W3, MDN, and M

4条回答
  •  情话喂你
    2020-12-15 04:20

    So, if your HTTP request method is GET, then the HTTP client sends data to the server simply by appending it to the request URL (as a so called query string), meaning that the body of the request is gonna stay empty and that is why you need to set the value of that argument to null.

    However, if your HTTP request method is POST, then your request data will be placed into the request body, which will be eventually sent to the server via send function.

    Cheers!

提交回复
热议问题