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:

namemeaning
headersan array of headers
postData

this can be:

  • a string: send it as is
  • an array of parameters: encode as form values
  • null/undefined: no POST data.

method

GET, POST or PUT (this is automatically set if postData exists).
onLoada function handle to call when the load is complete, it takes two parameters: the responseText and the XHR object.
onErrora function handle to call when an error occurs, it takes three  parameters: the error, the responseText and the XHR object.
loggeran 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:75 次

字数:3509

最后编辑:6年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文