{"id":22541335,"url":"https://github.com/pandaoh/js-xajax","last_synced_at":"2025-04-09T21:50:30.371Z","repository":{"id":38362438,"uuid":"492665143","full_name":"pandaoh/js-xajax","owner":"pandaoh","description":"JavaScript XHR\u0026AJAX. 封装 XMLHttpRequest(xhr)，方便需要使用原生 ajax 的项目使用。","archived":false,"fork":false,"pushed_at":"2024-01-24T02:14:04.000Z","size":32,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-04-25T11:03:20.220Z","etag":null,"topics":["ajax","js-ajax","js-xajax","js-xhr","request","x-request","xajax","xhr","xmlhttprequest","xrequest"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/js-xajax","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/pandaoh.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-05-16T02:57:31.000Z","updated_at":"2022-09-02T06:30:34.000Z","dependencies_parsed_at":"2024-12-07T12:15:32.537Z","dependency_job_id":null,"html_url":"https://github.com/pandaoh/js-xajax","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/pandaoh%2Fjs-xajax","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pandaoh%2Fjs-xajax/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pandaoh%2Fjs-xajax/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pandaoh%2Fjs-xajax/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pandaoh","download_url":"https://codeload.github.com/pandaoh/js-xajax/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248119404,"owners_count":21050754,"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","js-ajax","js-xajax","js-xhr","request","x-request","xajax","xhr","xmlhttprequest","xrequest"],"created_at":"2024-12-07T12:15:29.821Z","updated_at":"2025-04-09T21:50:30.352Z","avatar_url":"https://github.com/pandaoh.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# js-xajax\n\n\u003e JavaScript XMLHttpRequest(xhr) 的封装，方便需要使用原生 ajax 的项目使用。\n\n* 方便全局管理请求\n* 拆箱即用解决了原生 xhr 使用复杂的问题。\n* 支持请求超时设置\n* 支持自定义请求/响应数据处理\n* 支持异常处理与特定事件监听\n* 增加请求/响应/错误拦截功能\n* 请求头拦截处理(Headers)\n* 日志输出，请求完成时的回调(无论是否成功) Hooks。\n* 提供实例默认配置修改方法(timeout/baseUrl...)\n\n## Install\n\n```bash\nnpm install js-xajax -S\n```\n\n## Import\n\n```javascript\n// Es or Node\nconst { XAjax, HttpMethod, EVENTS } = require('js-xajax');\nimport { XAjax, HttpMethod, EVENTS } from 'js-xajax';\nimport XAjax from 'js-xajax';\n\n// Browser\n\u003cscript src=\"xajax.min.js\"\u003e\u003c/script\u003e\nconsole.log(xajax);\n```\n\n## Use\n\n```javascript\nconsole.log({ XAjax, HttpMethod, EVENTS });\n\nXAjax.get('https://a.biugle.cn', { q: 'query' }, { token: 'xxx' })\n  .then((data) =\u003e console.log(data))\n  .catch((e) =\u003e console.log(e));\n\n$xhr = XAjax.create({\n  dump: JSON.stringify, // 用户自定义数据 encode\n  load: JSON.parse, // 用户自定义数据 decode\n  xmlHttpRequest: () =\u003e new XMLHttpRequest(), // 生成 xmlHttpRequest 实例\n  timeout: 30000,\n  baseUrl: '',\n  raw: false, // 数据按原格式发送，不进行 dump/load\n  withCredentials: false, // Setting after open for compatibility with IE versions \u003c=10\n  eventsHandler: {\n    [EVENTS.PROGRESS]: (xhr, xhrProgressEvent) =\u003e {\n      console.log('xhr', xhr);\n      console.log('progress', xhrProgressEvent);\n    } // 事件监听处理\n  },\n  requestHandler: (request) =\u003e {\n    console.log('requestHandler', request);\n  },\n  setRequestHeaders: (headers) =\u003e {\n    console.log('setRequestHeaders', headers);\n    return headers;\n  },\n  responseHandler: (response) =\u003e {\n    console.log('responseHandler', response);\n  },\n  errorHandler: (error) =\u003e {\n    console.log('errorHandler', error);\n  },\n  requestFinally: () =\u003e {\n    console.log('requestFinally');\n  }\n});\n// use $xhr\n$xhr\n  .post('https://a.biugle.cn/post', { data: 'data' }, { token: 'xxx' })\n  .then((data) =\u003e console.log(data))\n  .catch((e) =\u003e console.log(e));\n```\n\n## API Docs\n\n[API Docs](https://github.com/pandaoh/js-xajax/blob/main/docs/README.md)\n\n## Others\n\n* [Issue](https://github.com/pandaoh/js-xajax/issues)\n* [Pull Request](https://github.com/pandaoh/js-xajax/pulls)\n* [hxbpandaoh@163.com](mailto:hxbpandaoh@163.com)\n* [参考项目](https://github.com/radiosilence/xr)\n* 待增加功能：\n  * 请求重试\n  * 自动转换回数据\n  * 取消重复请求/取消请求/请求白名单\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpandaoh%2Fjs-xajax","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpandaoh%2Fjs-xajax","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpandaoh%2Fjs-xajax/lists"}