Http.jsm 编辑
HTTP.jsm
Http.jsm provides httpRequest - a wrapper for XMLHttpRequest that provides convenient and simplified API for dealing with HTTP requests.
httpRequest supports the following parameters:
name | meaning |
---|---|
headers | an array of headers |
postData | this can be:
|
method | GET, POST or PUT (this is automatically set if postData exists). |
onLoad | a function handle to call when the load is complete, it takes two parameters: the responseText and the XHR object. |
onError | a function handle to call when an error occurs, it takes three parameters: the error, the responseText and the XHR object. |
logger | an object that implements the debug and log methods (e.g. log.jsm). |
Headers or post data are given as an array of arrays, for each inner array the first value is the key and the second is the value. For example: [["key1", "value1"], ["key2", "value2"]].
Submitting post data
httpRequest allows attaching data to the post requests. The data can be specified via postData option. postData can be of 2 different types: a string or an array of parameters. When null/undefined is given, no post data will be attached. If a string is given the data will be appended to the request as is. In case an array of parameters is given, it will be treated as an array of key-value pairs. The elements of the array will be URL-encoded and "application/x-www-form-urlencoded; charset=utf-8" will be enforced as the content type. Http.jsm only forces the content type if the post data is an Array, and it serializes it automatically. If the provided postdata is a string, the content type isn't touched. In this case, the content type may be set through the headers parameter.
Additional XHR configuration
It is possible to modify things manually on the XHR object after calling httpRequest. httpRequest returns an XHR object, which can be used to set additional parameters for the request. For example, XHR might be configured to use any custom MIME-type when processing a response regardless of what the server returns. To achieve this, you can obtain an XHR object by calling httpRequest and then call its overrideMimeType() with a preferred MIME-Type. Another example of how this can be used can be found here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论