Skip to content

fliptheweb/cross-domain-request

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fetch has a opaque mode by passing { mode: 'no-cors' } option. In that mode, you could achieve the same results (send a cross-domain request without access to a response) as with that library.

Cross-domain requests without CORS-support 🏓

Do you need to make a request (POST or GET) from browser to external resource without CORS?

If you don't have access for the requested resource and not able to setup the correct CORS policy, use jsonp or make postMessage transport – that simple, lightweight (2kb) and zero-dependency library could help you.

You could make an ajax-like cross-domain request, but without receiving the response, because of browser's XSS-protection.

Example (sandbox.io):

import CrossDomainRequest from 'cross-domain-request'

const data = {
  foo: 'bar'
}
CrossDomainRequest('https://reqbin.com/echo/post/form', data)

Parameters:

CrossDomainRequest(url, data, options)
  • url (string) - requested url for submit data;
  • data (Object);
  • options ([Object]) - options with method, eg GET ({ method: 'POST' } by default);

How it works and what's happened under the hood:

  • Make a hidden form;
  • Make an iframe and connect with the form by target attribute for prevent reload/open new browser tab;
  • Fill the form data and submit to passed url, but with target page won't be reloaded and result will be in the iframe;
  • Clean all artifacts.

TODO:

  • custom function for serialize nested data
  • tests
  • ability to pass FormData

References

  • post-robot - Cross-domain post-messaging library

About

Make POST/GET cross-domain requests without CORS

Resources

Stars

Watchers

Forks

Packages

No packages published