{"id":20839103,"url":"https://github.com/sbdavid/jsonpbugfree","last_synced_at":"2026-03-02T07:03:01.683Z","repository":{"id":57285818,"uuid":"127436103","full_name":"SBDavid/jsonpBugfree","owner":"SBDavid","description":"解决了jsonp请求并发时，相同jsonpCallback造成callback is not defined的错误","archived":false,"fork":false,"pushed_at":"2018-04-24T07:40:33.000Z","size":97,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-09T09:16:38.993Z","etag":null,"topics":["callback-is-not-defined","jquery","jsonp"],"latest_commit_sha":null,"homepage":null,"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/SBDavid.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":"2018-03-30T14:18:55.000Z","updated_at":"2019-04-16T09:26:20.000Z","dependencies_parsed_at":"2022-09-19T23:52:51.762Z","dependency_job_id":null,"html_url":"https://github.com/SBDavid/jsonpBugfree","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/SBDavid/jsonpBugfree","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SBDavid%2FjsonpBugfree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SBDavid%2FjsonpBugfree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SBDavid%2FjsonpBugfree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SBDavid%2FjsonpBugfree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SBDavid","download_url":"https://codeload.github.com/SBDavid/jsonpBugfree/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SBDavid%2FjsonpBugfree/sbom","scorecard":{"id":123793,"data":{"date":"2025-08-11","repo":{"name":"github.com/SBDavid/jsonpBugfree","commit":"a486dcc68f6908a1dc46da4e5161ced553b13cda"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":2.6,"checks":[{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Code-Review","score":0,"reason":"Found 0/13 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"SAST","score":0,"reason":"no SAST tool detected","details":["Warn: no pull requests merged into dev branch"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":0,"reason":"license file not detected","details":["Warn: project does not have a license file"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}}]},"last_synced_at":"2025-08-16T03:12:11.495Z","repository_id":57285818,"created_at":"2025-08-16T03:12:11.495Z","updated_at":"2025-08-16T03:12:11.495Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29994619,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-02T01:47:34.672Z","status":"online","status_checked_at":"2026-03-02T02:00:07.342Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["callback-is-not-defined","jquery","jsonp"],"created_at":"2024-11-18T01:12:21.584Z","updated_at":"2026-03-02T07:03:01.644Z","avatar_url":"https://github.com/SBDavid.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 如何解决jquery.jsonp方法在并发情况下容易发生异常的bug\n\n知道现在使用jsonp的公司越来越少了，似乎有比jsonp更好的跨域方案。但是我发现腾讯视屏、爱奇艺视频、优酷土豆等大型互联网公司还在使用它时，我决定写一篇文章彻底解决jsonp在并发条件下报错的问题。毕竟jsonp有最好的兼容性。\n\n# 1. 为什么会报错\n你是不是见过以下错误，通常在并发情况下容易出现这个错误，而且是否出错有随机性。\n```js \nUncaught ReferenceError: XXX is not defined\n```\n\n## 1.1 相同的jsonpCallback\n造成以上错误的原因是使用了相同的jsonpCallback，由于jquery将jsonpCallback赋值到window上，并且请求完成后会删除回调方法，所以当两个jsonp请求使用相同的jsonpCallback时就会造成冲突。\n\n## 1.2 每个请求都使用不同的jsonpCallback就没有问题吗？\n如果每个请求都使用不同的jsonpCallback是可以解决以上报错问题，但是这会引发服务端性能问题。因为服务端可以通过CDN对相同的请求进行缓存，如果每次jsonpCallback都不同，服务端缓存就会失效，流向直接冲击源站。所以对用相同参数的jsonp请求jsonpCallback必须是相同的。如果不同参数或url的jsonp请求使用了相同的jsonpCallback，我们可以在jsonpCallback后面加随机数。\n\n## 1.3 那完全相同的jsonp请求在并发时如何处理jsonpCallback冲突的BUG?\n如果出现这种情况，我们在发送请求前判断当前是否有相同的jsonp请求正在发送，如果发生冲突则取消本次请求，等到上一次请求完成后再重新请求。也就是说把并发的请求转换为穿行执行。\n\u003e 有些人说可以使用async直接把并行转为穿行，不好意思这个参数在jsonp中是无效的。\n\n## 1.4 一个隐秘的BUG\n上面提到的解决方案有一个隐秘的BUG。如果不使用jsonp方法返回的延迟对象的话，以上方法没有问题。但是如果使用了延迟对象，你会发现返回的defferd对象可能永远无法触发resolve或者reject。这是应为在发现冲突后jsonp请求被取消，所以请求根本没有发出，jsonp根本不返回defferd对象。这个问题也不难解决，我们可以定义自己的defferd对象，当请求完成后手动触发resolve或者reject。这一切都已经在最终的解决方案中实现了。\n\n# 2. 如何使用呢？\n\n## 2.0 安装\nnpm i jsonp-bugfree\n\n## 2.1 依赖\n- jquery 这个不用说，依赖$.ajax()方法\n- md5 其实这个不是必须的，使用它的目的是为了判断请求的url和参数是否和其他请求相同。因为url+参数可能是一个非常长的字符串，使用md5可以控制字符长度。如果你不想使用md5也可以使用其他方法代替。\n\n## 2.2 引入模块\n支持amd、commonjs、script标签直接引用。\n\n## 2.3 demo\n```js\nvar myjsonp = new jsonpBugfree();\n\nmyjsonp.JSONP({\n                url: 'http://url',\n                data: {\n                    test: 1\n                },\n                cache: true,\n                dataType: 'jsonp',\n                jsonp: 'cb',\n                jsonpCallback: 'jsonpCallback',\n                success: function () {\n\n                }\n            })\n            .then(function () {\n                // todo\n            })\n            .catch(function () {\n                // todo\n            })\n\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsbdavid%2Fjsonpbugfree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsbdavid%2Fjsonpbugfree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsbdavid%2Fjsonpbugfree/lists"}