简体   繁体   中英

JavaScript: Shall I pass null into XMLHttpRequest.send()?

I sometimes see in other peoples code that they pass null to the send-method of XMLHttpRequest.

req.send(null)

Others call send() with empty parenthesis.

Obviously the parameter is meant to pass parameter to a post-request.

Does it make any sense to write it when I'm doing get-requests?

Once somebody told me that it could cause problems NOT passing null in there. But without being able to explain the whole issue properly. :))

Is it true that it can cause problems to leave the parameter out?

Specifying null for the body parameter or leaving it out entirely causes exactly the same behaviour. The spec shows how the default value if you don't specify body is null :

 client . send([body = null]) 

and step 4 shows how the entire step is skipped if body is null , which it will be whether you leave it out, or explicitly set it to null :

  1. If body is null, go to the next step.

Otherwise it carries on with the creation of the request body, based on what you've passed in. If you're only doing GET requests, leaving it out is fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM