{"id":15834915,"url":"https://github.com/kaelzhang/request","last_synced_at":"2025-08-01T09:34:39.530Z","repository":{"id":9598624,"uuid":"11519447","full_name":"kaelzhang/request","owner":"kaelzhang","description":null,"archived":false,"fork":false,"pushed_at":"2014-04-29T09:38:15.000Z","size":328,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-25T13:16:19.116Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/kaelzhang.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-MIT","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-07-19T03:35:44.000Z","updated_at":"2015-01-28T09:17:05.000Z","dependencies_parsed_at":"2022-08-30T10:20:34.642Z","dependency_job_id":null,"html_url":"https://github.com/kaelzhang/request","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/kaelzhang%2Frequest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaelzhang%2Frequest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaelzhang%2Frequest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaelzhang%2Frequest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kaelzhang","download_url":"https://codeload.github.com/kaelzhang/request/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246419966,"owners_count":20774191,"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":[],"created_at":"2024-10-05T14:02:29.255Z","updated_at":"2025-04-01T12:28:07.744Z","avatar_url":"https://github.com/kaelzhang.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# request\n\nSend ajax or jsonp request to server\n\n## Installation\n\n``` bash\ncortex install request --save\n```\n\n## Synopsis\n\n``` js\nvar request = require('request');\nvar Ajax = request.Ajax;\n\nnew Ajax({\n    url: http://example.com/data.json',\n    method: 'post',\n    dataType: 'json'\n}).on(\"success\",succ_cb)\n.on(\"cacnel\", cancel_cb)\n.send(data);\n\nnew Ajax({\n    url: http://example.com/data.txt',\n    dataType: 'text',\n    parse: function(data) {\n       return data.obj;\n    },\n    data: {}\n}).on(\"success\", succ_cb)\n.on(\"error\", err_cb)\n.send();\n\n\nvar JSONP = require.JSONP;\n\nnew JSONP({\n    url: '/blah-blah-blah.jsonp',\n    data: {\n        uid: 123\n    }\n}).on(\"success\", succ_cb)\n.on(\"error\", err_cb)\n.send();\n```\n\n\n## API Documentation\n\n### Ajax\n\n#### Constructor: new Ajax(options)\n\nCreate Ajax object.\n\n##### Regular Options\n\n* url `string` request address\n* method `string=` The way to send request, can be either 'GET' or 'POST', case-insensitive\n* data `object=` The data send in request body\n\n##### Advanced Options\n\n* parser `function(data)=` The method will be called after 'JSON.parse' but before the 'success' event, it will try to transform the data.\n* santitizer `function(responseText)=` Handle the original response object, it will return original response by default.\n* isXHRSuccess `function(xhr)=` Return a boolean value indicates whether the request succeed or fail. Default it will treat any status code between 200 and 300 as success.\n* isSuccess `function(response)` Accept response as arguments and return whether the request succeed or fail. By default it will always return true. Take ajax service in DP for example: most of time, we treat response as success only when `data.code` is 200, but the ajax request actually runs well, so the error callback won't be get called. So we add function like: ```function(res) { return !!res \u0026\u0026 res.code === 200; }```, the any response whose code is not equal to 200 will be treated as `error`.\n\n##### Other Options\n\n* dataType `string=` Default return dataType, default is ='json'=. It will affect the `Content-Type` in header.\n* async `boolean=true` Whether is an async request，Default set to `true`\n* timeout `number=` Timeout for the request, in millisecond, when setting to 0, it will never timeout. Default is 0.\n* headers `Object=` Custom headers, the final header may be overwritten by other options.\n* cache `boolean=false` Set to false by default, which prevents browser to cache the 'GET\" Ajax request.\n\n#### Method: ajax.cancel()\n\nCancel ajax request, will trigger 'cancel' event. Return ajax object itself for chaining.\n\n#### Method: ajax.send([data])\n\nSend the ajax request with or without data. Return ajax object itself for chaining.\n\n#### Event: ajax#success\n\nTriggered when ajax request succeed.\n\n##### callback(response, xhr)\n\n* resposne `object|string` response body.\n* xhr `object` XMLHttpRequest Object.\n\n#### Event: ajax#error\n\nTriggered when ajax request failed or timeout.\n\n##### callback(response, xhr)\n\nThe same as ajax#success.\n\n\n#### Event: ajax#cancel\n\nTriggered when ajax request get canceled. =callback= has no argument.\n\n\n\n### JSONP\n\n#### Constructor: new JSONP(options)\n\nCreate a JOSONP object.\n\n\n##### options\n\n* url `string`  Address requested\n* data `(Object|string)=` Query that send with JSONP，can be querystring or object\n* callbackKey `string=` Name of parameter that tells server the callback function name, default is 'callback'\n* timeout `number=` Timeout for the request, in millisecond, when setting to 0, it will never timeout. Default is 0.\n* cache `boolean=false` Set to false by default, which prevents browser to cache the jsonp request.\n\n\n#### Method: jsonp.concal()\n\nCancel jsonp request, will trigger 'cancel' event. Return jsonp object itself for chaining.\n\n#### Method: jsonp.send([data])\n\nSend the jsonp request with or without data. Return jsonp object itself for chaining.\n\n#### Event: jsonp#success\n\nTriggered when jsonp request succeed.\n\n##### callback(response, xhr)\n\n* resposne `object|string` response body.\n* xhr `object` XMLHttpRequest Object.\n\n#### Event: jsonp#error\n\nTriggered when jsonprequest failed or timeout.\n\n##### callback(response, xhr)\n\nThe same as jsonp#success.\n\n\n#### Event: jsonp#cancel\n\nTriggered when jsonp request get canceled. =callback= has no argument.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaelzhang%2Frequest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkaelzhang%2Frequest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaelzhang%2Frequest/lists"}