{"id":21557084,"url":"https://github.com/shenfe/rext","last_synced_at":"2025-04-10T10:32:01.648Z","repository":{"id":65489963,"uuid":"102376434","full_name":"shenfe/rext","owner":"shenfe","description":"🎈A lightweight (\u003c 5kb gzipped) and Promise-supported HTTP request library, for all browsers.","archived":false,"fork":false,"pushed_at":"2018-05-13T14:40:04.000Z","size":149,"stargazers_count":15,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-24T09:21:09.549Z","etag":null,"topics":["ajax","cross-domain","iframe","promise","xdomainrequest","xhr"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shenfe.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-09-04T15:34:53.000Z","updated_at":"2023-11-07T12:46:31.000Z","dependencies_parsed_at":"2023-01-25T17:45:15.854Z","dependency_job_id":null,"html_url":"https://github.com/shenfe/rext","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shenfe%2Frext","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shenfe%2Frext/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shenfe%2Frext/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shenfe%2Frext/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shenfe","download_url":"https://codeload.github.com/shenfe/rext/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248084623,"owners_count":21045132,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ajax","cross-domain","iframe","promise","xdomainrequest","xhr"],"created_at":"2024-11-24T08:10:53.121Z","updated_at":"2025-04-10T10:32:01.617Z","avatar_url":"https://github.com/shenfe.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"- [rext](#)\n\t- [Quick Import](#quick-import)\n\t- [IE 9- Support](#ie-9--support)\n\t- [API](#api)\n\t\t- [XMLHttpRequest](#xmlhttprequest)\n\t\t- [JSONP](#jsonp)\n\t\t- [Promise Object Returned](#promise-object-returned)\n\t- [Options](#options)\n\t- [Case Matrix](#case-matrix)\n\t- [Take a Look at headers['Content-Type']](#take-a-look-at-headerscontent-type)\n\t- [Take a Look at responseType](#take-a-look-at-responsetype)\n\t- [License](#license)\n\n\u003ch1 align=\"center\"\u003erext\u003c/h1\u003e\n\n![gzip size](http://img.badgesize.io/https://raw.githubusercontent.com/shenfe/rext/master/dist/rext.min.js?compression=gzip)\n\u003ca href=\"https://www.npmjs.com/package/rexter\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/rexter.svg\"\u003e\u003c/a\u003e\n![license](https://img.shields.io/npm/l/rexter.svg)\n\nA lightweight (\u003c 5kb gzipped) and **Promise-supported** HTTP request library, for all browsers (i.e. even cross-domain requests in IE 6 are possible).\n\n| \u003cimg src=\"https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/edge.png\" alt=\"IE / Edge\" width=\"16px\" height=\"16px\" /\u003e IE / Edge | \u003cimg src=\"https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/chrome.png\" alt=\"Chrome\" width=\"16px\" height=\"16px\" /\u003e Chrome | \u003cimg src=\"https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/firefox.png\" alt=\"Firefox\" width=\"16px\" height=\"16px\" /\u003e Firefox | \u003cimg src=\"https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/safari.png\" alt=\"Safari\" width=\"16px\" height=\"16px\" /\u003e Safari |\n| :---: | :---: | :---: | :---: |\n| 6+ ✔ | All ✔ | All ✔ | All ✔ |\n\n## Quick Import\n\n```bash\n$ npm install --save rexter\n```\n\n```js\nvar rext = require('rexter');\n```\n\n## IE 9- Support\n\nIf IE9- browsers are required to **send cross-domain requests with user credentials**, define the invoker-hostname whitelist in `dist/iframe-agent.html` and put it at the root path of the target origin.\n\nFor example:\n\nDefine the invoker-hostname whitelist in `iframe-agent.html`:\n\n\u003e Replace\n\u003e ```js\n\u003e [/* Define a whitelist of host names here, e.g. '.invoker.com'. */]\n\u003e ```\n\u003e with\n\u003e ```js\n\u003e ['.my-domain.com', '.my-domain1.com']\n\u003e ```\n\nAdditianally, before `rext` is imported, the JSON polyfill (e.g. [JSON-js](https://github.com/douglascrockford/JSON-js)) shall be executed.\n\n⚠️ `rext` does not include full polyfills in itself.\n\n## API\n\nAs simple as `rext(options)`.\n\nBesides, the format of jQuery ajax's option object is also allowed:\n\n```js\n{\n    type: 'post',\n    url: '/path/to/api',\n    data: {},\n    contentType: 'application/json',\n    dataType: 'json',\n    xhrFields: {\n        withCredentials: true\n    },\n    success: function () { /**/ },\n    error: function () { /**/ },\n    complete: function () { /**/ }\n}\n```\n\n### XMLHttpRequest\n\nThe `success`, `error`, `always` callbacks are allowed both a chain call and defined in the option object.\n\n```js\nrext({\n    url: '/path/to/resource',\n    data: { /**/ },\n    promise: false\n}).success((data, response) =\u003e {\n    /**/\n}).error((data, response) =\u003e {\n    /**/\n}).always((data, response) =\u003e {\n    /**/\n});\n```\n\n### JSONP\n\nThe callback function is allowed either as the second parameter behind the option object or defined in the option object.\n\n```js\nrext({\n    jsonp: true,\n    url: '/path/to/resource',\n    data: { /**/ }\n}, data =\u003e {\n    /**/\n});\n```\n\n### Promise Object Returned\n\nA Promise object is returned by default. Set property `promise` value `false` if you do not want to get a Promise object or you prefer to use `success` and `error` methods actually.\n\n```js\nrext({\n    url: '/path/to/resource',\n    data: { /**/ }\n}).then(data =\u003e {\n    /**/\n}).catch(err =\u003e {\n    /**/\n});\n```\n\n👏 [shenfe/promises-aplus](https://github.com/shenfe/promises-aplus) is used as a polyfill when the browser does not support Promise.\n\n## Options\n\nInstructions of the option object:\n\n| Property | Type | Value |\n| :---: | :---: | :--- |\n| `type` | `{Undefined\\|String}` | 'get' (default), 'post'. |\n| `url` | `{String}` | The resource url string. |\n| `data` | `{Undefined\\|Object}` | The data to send. Object recommended. |\n| `withCredentials` | `{Undefined\\|Boolean}` | undefined (false, as default), true. The `withCredentials` property of the request. Whether to send user credentials with the request to another origin or not. An `xhrFields` object with `withCredentials` property of value `true` is accepted as well. |\n| `agent` | `{Undefined\\|Boolean}` | undefined (false, as default), true. Whether to fall back to the iframe agent directly when the request is cross-domain and the browser is IE 9-. |\n| `agentPageUrl` | `{Undefined\\|String}` | undefined (\\`${targetOrigin}/iframe-agent.html\\`, as default), a url string. Specify the url of the iframe agent page if it's not in the root path of the target origin. |\n| `responseType` (or `dataType`) | `{Undefined\\|String}` | 'text' (default), 'json', .etc. Similar to the `dataType` option in jQuery ajax. A simple trial of JSON parsing would be conducted upon the response data besides the MIME type. See below for more. |\n| `headers` | `{Undefined\\|Object}` | The request headers object. Usually used to define the `Content-Type` property (similar to the `contentType` option in jQuery ajax), of which 'application/x-www-form-urlencoded' is the default value. See below for more. |\n| `contentType` | `{Undefined\\|String}` | The same as `headers['Content-Type']`. |\n| `jsonp` | `{Undefined\\|Boolean}` | undefined (false, as default), true. The same as setting `responseType` (or `dataType`) `jsonp`. |\n| `promise` | `{Undefined\\|Boolean\\|Function}` | undefined, false, true, or a Promise object constructor. Whether to return a Promise object. Set it `false` if not for a Promise object. |\n| `simple` | `{Undefined\\|Boolean}` | undefined (false, as default), true. Whether not to force an X-Requested-With header in an XHR. |\n\n## Case Matrix\n\nAll the cases of browser requests.\n\n| Cross-Domain | With-Credentials | Web Browser | Approach | Restriction | Security |\n| :---: | :---: | :---: | :---: | :--- | :--- |\n| no | - | IE 6- | ActiveXObject | Almost the same API as the XMLHttpRequest Object. | - |\n| no | - | IE 7-9 | XMLHttpRequest | [The XMLHttpRequest Object](https://www.w3.org/TR/2006/WD-XMLHttpRequest-20060405/). | - |\n| yes | no | IE 8-9 | XDomainRequest | [XDomainRequest - Restrictions, Limitations and Workarounds](https://blogs.msdn.microsoft.com/ieinternals/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds/) | - |\n| no | - | IE 10-11, non-IE | XMLHttpRequest (Level 2) | [XMLHttpRequest Level 2](https://xhr.spec.whatwg.org/). IE 10-11 do not support value `json` as XHR's `responseType`, but it doesn't matter. | - |\n| yes | - | IE 10-11, non-IE | XMLHttpRequest (Level 2) | Server responses should include the `Access-Control-Allow-Origin` HTTP response header with value `*`, or the exact origin of the calling page. | - |\n| yes | yes | IE 10-11, non-IE | XMLHttpRequest (Level 2) | Server responses should include the `Access-Control-Allow-Origin` HTTP response header with the exact origin of the calling page, and the `Access-Control-Allow-Credentials` HTTP response header with value `true`. | - |\n| - | - | - | JSONP | - | [Security concerns](https://en.wikipedia.org/wiki/JSONP#Security_concerns) |\n| - | - | - | iframe agent | Be put in a specific place of the target origin. | A whitelist of visitor origins is required. |\n\n## Take a Look at `headers['Content-Type']`\n\nThe MIME type of data to **send**, like the `contentType` in jQuery ajax.\n\n| Value | Effect |\n| :---: | :--- |\n| `application/x-www-form-urlencoded` | The default, recommended. Almost the same as the url query string. |\n| `multipart/form-data` | [HTML 4 specification](https://www.w3.org/TR/html401/interact/forms.html#h-17.13.4). This allows entire files to be included in the data. Use this when the form includes any `\u003cinput type=\"file\"\u003e` element. In this case, `Content-Type` would be ignored because the browser set this header with a boundary generated by itself. |\n| `text/plain` | [HTML 5 specification](https://www.w3.org/TR/html5/forms.html#text/plain-encoding-algorithm). Not recommended. Do not use it unless for debugging. |\n| `application/json` | **Personally** not recommended for common POST requests. Make sure you really need to post complex data with user credentials. Additionally, for CORS requests, setting the content type to anything other than `application/x-www-form-urlencoded`, `multipart/form-data`, or `text/plain` will trigger the browser to send a preflight OPTIONS request to the server. |\n\n## Take a Look at `responseType`\n\nThe alias of the expected MIME type of data to **receive**, similar to the `dataType` in jQuery ajax. This option should affect the request header `Accept` and how to treat the response data, relating the response header `Content-Type`. However, **a simple trial of JSON parsing** would be conducted then regardless of the type of response data.\n\nAccording to the [specification](https://xhr.spec.whatwg.org/#the-responsetype-attribute) of `responseType`, the value can be:\n\n| Value | Description |\n| :---: | :--- |\n| text | Default. |\n| json | - |\n| blob | If to receive files. |\n| arraybuffer | If to receive files. |\n| document | Seldom. |\n\n## License\n\n[MIT](http://opensource.org/licenses/MIT)\n\nCopyright © 2017-present, [shenfe](https://github.com/shenfe)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshenfe%2Frext","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshenfe%2Frext","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshenfe%2Frext/lists"}