最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - disable CORS checking in jsdom on nodejs - Stack Overflow

programmeradmin6浏览0评论

Is it possible to disable the CORS checking functionality in the jsdom node module?

The CORS errors are particularly difficult to debug in my use case (clientside testing) and do not occur in the actual environment I am running the program.

Perhaps some environmental variable we can set when initializing the object? e.g.,

window = new jsdom.JSDOM(``,{
    cors : false,
}).window;

Is it possible to disable the CORS checking functionality in the jsdom node module?

The CORS errors are particularly difficult to debug in my use case (clientside testing) and do not occur in the actual environment I am running the program.

Perhaps some environmental variable we can set when initializing the object? e.g.,

window = new jsdom.JSDOM(``,{
    cors : false,
}).window;
Share Improve this question edited Mar 23, 2018 at 15:43 Vadim Kotov 8,2848 gold badges50 silver badges63 bronze badges asked Mar 23, 2018 at 15:40 Ulad KasachUlad Kasach 12.9k13 gold badges69 silver badges90 bronze badges 8
  • what does jsdom have to do with cors/ajax? doesn't it just mimick the DOM? – Kevin B Commented Mar 23, 2018 at 15:45
  • @KevinB jsdom implements XMLHttpRequest and other API's found in the browser. – Ulad Kasach Commented Mar 23, 2018 at 15:47
  • Right, but why would that be throwing CORS errors? – Kevin B Commented Mar 23, 2018 at 15:48
  • @KevinB It allows a url parameter to be passed as an option for which it then considers same-origin restrictions – Ulad Kasach Commented Mar 23, 2018 at 15:50
  • 1 It's more about the server not allowing you to request from a client that is not on the same origin. This issue seems to confirm that's it's not possible in JSDOM. – Pierre C. Commented Mar 23, 2018 at 15:54
 |  Show 3 more ments

3 Answers 3

Reset to default 6

As stated in this issue on JSDOM Github repository, JSDOM acts like a browser and the same-origin policy applies.

It cannot be disabled from there.

The solution would be to add headers to the resource server allowing the origin of the client which makes the request.

Just set testURL in your jest.config.js to your API address.

Set window._origin to the requested origin such as http://www.example.. Works for Jest 27.5.1.

发布评论

评论列表(0)

  1. 暂无评论