简体   繁体   中英

what kind of parameters can I pass to XMLHttpRequest.send()?

I have a request that looks like this

var xhr = new XMLHttpRequest();
[...]
xhr.send("param1=" + obj.param1 + "&param2=" + val);

I'd really prefer to write something like

xhr.send( {"param1": obj.param1, "param2": val} );

(A) Is it possible to do that?

(B) Where can I find a spec that would answer that question?

Is it possible to do that?

Not currently, but such things are included in the draft: http://www.w3.org/TR/XMLHttpRequest/#the-send-method . XmlHttpRequest should be able to send ArrayBuffers, Blobs, Documents, strings and FormData objects ( examples at html5rocks ).

However, sending objects is not supported - they'd need to be serializable. You could send them as a JSON string, or use one of the many library function to generate URL argument strings (like jQuery.param() ).

Where can I find a spec that would answer that question?

I've linked the spec above, though for browser support you'd need to look at compatibility tables or the browsers' documentations (eg. for FF at MDN ).

你可以使用jQuery轻松完成这类工作: jQuery.ajax({ url: http://yoururl.com, data: {"param1": obj.param1, "param2": val}});

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