Simple solution to cross domain errors
posted 2020.04.26 by Clark Wilkins, SImplexable

This is something that I ran into problems with in the following situation.

I was tasked with abstracting some general resources from our commercial platforms to a single source. One of these resources is a Jquery-based handler script that interfaces with our new AWS-based APIs to work with customer information. The goal here is to share the customer data across multiple platforms: stockd, servicd, etc.

I wrote the core setup code to call a handler script that resides on global.simplexable.com, but when the handler was executed, I got cross-domain errors.

After reading a lot of obscure documentation, most of which was totally outdated, I came up with the solution.

PART 1: enable cross-domain on the browser's query

  $.ajax ( {

url: "[my handler URL]",

data: { [my flags] },
crossDomain: true,
type: "POST",
dataType : "html"

} )

PART 2: allow cross-domain by setting the header on the handler URL

In the handler called here as url, I added this line to set the header we need.

header ( 'Access-Control-Allow-Origin: *' );

Problem solved! The reason this makes so much difference to us is we can now set up a single source for site setup scripts that are useful in multiple platforms. We have a single API interface solution and storage backend for customer accounts that's accessible across all of them. One code base for handlers, display logic, etc.

Credit to this excellent resource for most of the solution. It's a bit out of date, and mine is simpler, if not particularly "elegant". :-)