{"id":13475210,"url":"https://github.com/ded/reqwest","last_synced_at":"2025-05-14T03:08:18.058Z","repository":{"id":44615907,"uuid":"1557647","full_name":"ded/reqwest","owner":"ded","description":"browser asynchronous http requests","archived":false,"fork":false,"pushed_at":"2021-10-03T11:58:08.000Z","size":9659,"stargazers_count":2938,"open_issues_count":107,"forks_count":340,"subscribers_count":91,"default_branch":"master","last_synced_at":"2025-04-10T13:35:38.462Z","etag":null,"topics":["ajax","javascript","jquery","jsonp","reqwest"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ded.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2011-04-01T22:11:21.000Z","updated_at":"2025-03-21T10:19:34.000Z","dependencies_parsed_at":"2022-09-11T06:51:18.613Z","dependency_job_id":null,"html_url":"https://github.com/ded/reqwest","commit_stats":null,"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ded%2Freqwest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ded%2Freqwest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ded%2Freqwest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ded%2Freqwest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ded","download_url":"https://codeload.github.com/ded/reqwest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248261933,"owners_count":21074225,"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","javascript","jquery","jsonp","reqwest"],"created_at":"2024-07-31T16:01:18.361Z","updated_at":"2025-04-10T17:18:44.567Z","avatar_url":"https://github.com/ded.png","language":"JavaScript","readme":"# It's AJAX\n\nAll over again. Includes support for xmlHttpRequest, JSONP, CORS, and CommonJS Promises A.\n\nIt is also isomorphic allowing you to `require('reqwest')` in `Node.js` through the peer dependency [xhr2](https://github.com/pwnall/node-xhr2), albeit the original intent of this library is for the browser. For a more thorough solution for Node.js, see [mikeal/request](https://github.com/request/request).\n\n## API\n\n``` js\nreqwest('path/to/html', function (resp) {\n  qwery('#content').html(resp)\n})\n\nreqwest({\n    url: 'path/to/html'\n  , method: 'post'\n  , data: { foo: 'bar', baz: 100 }\n  , success: function (resp) {\n      qwery('#content').html(resp)\n    }\n})\n\nreqwest({\n    url: 'path/to/html'\n  , method: 'get'\n  , data: [ { name: 'foo', value: 'bar' }, { name: 'baz', value: 100 } ]\n  , success: function (resp) {\n      qwery('#content').html(resp)\n    }\n})\n\nreqwest({\n    url: 'path/to/json'\n  , type: 'json'\n  , method: 'post'\n  , error: function (err) { }\n  , success: function (resp) {\n      qwery('#content').html(resp.content)\n    }\n})\n\nreqwest({\n    url: 'path/to/json'\n  , type: 'json'\n  , method: 'post'\n  , contentType: 'application/json'\n  , headers: {\n      'X-My-Custom-Header': 'SomethingImportant'\n    }\n  , error: function (err) { }\n  , success: function (resp) {\n      qwery('#content').html(resp.content)\n    }\n})\n\n// Uses XMLHttpRequest2 credentialled requests (cookies, HTTP basic auth) if supported\nreqwest({\n    url: 'path/to/json'\n  , type: 'json'\n  , method: 'post'\n  , contentType: 'application/json'\n  , crossOrigin: true\n  , withCredentials: true\n  , error: function (err) { }\n  , success: function (resp) {\n      qwery('#content').html(resp.content)\n    }\n})\n\nreqwest({\n    url: 'path/to/data.jsonp?callback=?'\n  , type: 'jsonp'\n  , success: function (resp) {\n      qwery('#content').html(resp.content)\n    }\n})\n\nreqwest({\n    url: 'path/to/data.jsonp?foo=bar'\n  , type: 'jsonp'\n  , jsonpCallback: 'foo'\n  , jsonpCallbackName: 'bar'\n  , success: function (resp) {\n      qwery('#content').html(resp.content)\n    }\n})\n\nreqwest({\n    url: 'path/to/data.jsonp?foo=bar'\n  , type: 'jsonp'\n  , jsonpCallback: 'foo'\n  , success: function (resp) {\n      qwery('#content').html(resp.content)\n    }\n  , complete: function (resp) {\n      qwery('#hide-this').hide()\n    }\n})\n```\n\n## Promises\n\n``` js\nreqwest({\n    url: 'path/to/data.jsonp?foo=bar'\n  , type: 'jsonp'\n  , jsonpCallback: 'foo'\n})\n  .then(function (resp) {\n    qwery('#content').html(resp.content)\n  }, function (err, msg) {\n    qwery('#errors').html(msg)\n  })\n  .always(function (resp) {\n    qwery('#hide-this').hide()\n  })\n```\n\n``` js\nreqwest({\n    url: 'path/to/data.jsonp?foo=bar'\n  , type: 'jsonp'\n  , jsonpCallback: 'foo'\n})\n  .then(function (resp) {\n    qwery('#content').html(resp.content)\n  })\n  .fail(function (err, msg) {\n    qwery('#errors').html(msg)\n  })\n  .always(function (resp) {\n    qwery('#hide-this').hide()\n  })\n```\n\n``` js\nvar r = reqwest({\n    url: 'path/to/data.jsonp?foo=bar'\n  , type: 'jsonp'\n  , jsonpCallback: 'foo'\n  , success: function () {\n      setTimeout(function () {\n        r\n          .then(function (resp) {\n            qwery('#content').html(resp.content)\n          }, function (err) { })\n          .always(function (resp) {\n             qwery('#hide-this').hide()\n          })\n      }, 15)\n    }\n})\n```\n\n## Options\n\n  * `url` a fully qualified uri\n  * `method` http method (default: `GET`)\n  * `headers` http headers (default: `{}`)\n  * `data` entity body for `PATCH`, `POST` and `PUT` requests. Must be a query `String` or `JSON` object\n  * `type` a string enum. `html`, `xml`, `json`, or `jsonp`. Default is inferred by resource extension. Eg: `.json` will set `type` to `json`. `.xml` to `xml` etc.\n  * `contentType` sets the `Content-Type` of the request. Eg: `application/json`\n  * `crossOrigin` for cross-origin requests for browsers that support this feature.\n  * `success` A function called when the request successfully completes\n  * `error` A function called when the request fails.\n  * `complete` A function called whether the request is a success or failure. Always called when complete.\n  * `jsonpCallback` Specify the callback function name for a `JSONP` request. This value will be used instead of the random (but recommended) name automatically generated by reqwest.\n\n## Security\n\nIf you are *still* requiring support for IE6/IE7, consider including [JSON3](https://bestiejs.github.io/json3/) in your project. Or simply do the following\n\n``` html\n\u003cscript\u003e\n(function () {\n  if (!window.JSON) {\n    document.write('\u003cscr' + 'ipt src=\"http://cdnjs.cloudflare.com/ajax/libs/json3/3.3.2/json3.min.js\"\u003e\u003c\\/scr' + 'ipt\u003e')\n  }\n}());\n\u003c/script\u003e\n```\n\n\n## Contributing\n\n``` sh\n$ git clone git://github.com/ded/reqwest.git reqwest\n$ cd !$\n$ npm install\n```\n\nPlease keep your local edits to `src/reqwest.js`.\nThe base `./reqwest.js` and `./reqwest.min.js` will be built upon releases.\n\n## Running Tests\n\n``` sh\nmake test\n```\n\n## Browser support\n\n  * IE6+\n  * Chrome 1+\n  * Safari 3+\n  * Firefox 1+\n  * Opera\n\n## Ender Support\nReqwest can be used as an [Ender](http://enderjs.com) module. Add it to your existing build as such:\n\n    $ ender add reqwest\n\nUse it as such:\n\n``` js\n$.ajax({ ... })\n```\n\nSerialize things:\n\n``` js\n$(form).serialize() // returns query string -\u003e x=y\u0026...\n$(form).serialize({type:'array'}) // returns array name/value pairs -\u003e [ { name: x, value: y}, ... ]\n$(form).serialize({type:'map'}) // returns an object representation -\u003e { x: y, ... }\n$(form).serializeArray()\n$.toQueryString({\n    foo: 'bar'\n  , baz: 'thunk'\n}) // returns query string -\u003e foo=bar\u0026baz=thunk\n```\n\nOr, get a bit fancy:\n\n``` js\n$('#myform input[name=myradios]').serialize({type:'map'})['myradios'] // get the selected value\n$('input[type=text],#specialthing').serialize() // turn any arbitrary set of form elements into a query string\n```\n\n## ajaxSetup\nUse the `request.ajaxSetup` to predefine a data filter on all requests. See the example below that demonstrates JSON hijacking prevention:\n\n``` js\n$.ajaxSetup({\n  dataFilter: function (response, type) {\n    if (type == 'json') return response.substring('])}while(1);\u003c/x\u003e'.length)\n    else return response\n  }\n})\n```\n\n## RequireJs and Jam\nReqwest can also be used with RequireJs and can be installed via jam\n\n```\njam install reqwest\n```\n\n```js\ndefine(function(require){\n  var reqwest = require('reqwest')\n});\n```\n\n## spm\nReqwest can also be installed via spm [![](http://spmjs.io/badge/reqwest)](http://spmjs.io/package/reqwest)\n\n```\nspm install reqwest\n```\n\n## jQuery and Zepto Compatibility\nThere are some differences between the *Reqwest way* and the\n*jQuery/Zepto way*.\n\n### method ###\njQuery/Zepto use `type` to specify the request method while Reqwest uses\n`method` and reserves `type` for the response data type.\n\n### dataType ###\nWhen using jQuery/Zepto you use the `dataType` option to specify the type\nof data to expect from the server, Reqwest uses `type`. jQuery also can\nalso take a space-separated list of data types to specify the request,\nresponse and response-conversion types but Reqwest uses the `type`\nparameter to infer the response type and leaves conversion up to you.\n\n### JSONP ###\nReqwest also takes optional `jsonpCallback` and `jsonpCallbackName`\noptions to specify the callback query-string key and the callback function\nname respectively while jQuery uses `jsonp` and `jsonpCallback` for\nthese same options.\n\n\nBut fear not! If you must work the jQuery/Zepto way then Reqwest has\na wrapper that will remap these options for you:\n\n```js\nreqwest.compat({\n    url: 'path/to/data.jsonp?foo=bar'\n  , dataType: 'jsonp'\n  , jsonp: 'foo'\n  , jsonpCallback: 'bar'\n  , success: function (resp) {\n      qwery('#content').html(resp.content)\n    }\n})\n\n// or from Ender:\n\n$.ajax.compat({\n  ...\n})\n```\n\nIf you want to install jQuery/Zepto compatibility mode as the default\nthen simply place this snippet at the top of your code:\n\n```js\n$.ajax.compat \u0026\u0026 $.ender({ ajax: $.ajax.compat });\n```\n\n\n**Happy Ajaxing!**\n","funding_links":[],"categories":["JavaScript","请求处理","HTTP Client/Request","24. Ajax模块","24. 前后端交互","前端 - broswer端"],"sub_categories":["redux 扩展","IDE","13.20 视差滚动(Parallax Scrolling) ###","调试","24.1 Ajax模块","数据"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fded%2Freqwest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fded%2Freqwest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fded%2Freqwest/lists"}