I'm struggling to figure out how to enable CORS
while using Ajax
to send data to a remote server on a different domain. Despite researching extensively and reading numerous Stackoverflow threads, I can't seem to make sense of it all. I understand that in order to bypass the browser's security restrictions, I need to include an Access-Control-Allow-Origin: *
header in my code, but where exactly should this go? I've come across suggestions to add this header as a simple tag in a PHP file and then utilize curls to create a POST request, but my expertise lies more in JavaScript than PHP. Can anyone provide guidance on how to approach this issue?
It's worth noting that I don't have the ability to configure or modify the server settings, so any solutions must be implemented on the client side.
Here is the snippet of code I currently have. While I was able to successfully send this message using a Chrome plugin, I understand that this workaround is not ideal.
$.ajax({
type: "POST",
url: "someURL",
data: {name: "John", lastName : "Johnson", state : "NYC" },
headers: {
"Access-Control-Allow-Origin: *"
}
})
.done(function( msg ) {
console.log(msg);
});