K
K
Konstantin2014-06-27 04:29:38
JavaScript
Konstantin, 2014-06-27 04:29:38

How to make an AJAX request from one subdomain to another?

Hello.
I am somewhat behind the times and modern technologies and I am trying to understand: is it possible in 2014 to make an Ajax request from one of my subdomains to another?
Browsers are modern, there is full control over subdomains (including headers).
Hacks with iframes, jsonp, images with get parameters, etc. I know perfectly well. So the question is only about AJAX and CORS.
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
@
@antoo, 2014-06-27
_

So you wrote everything correctly, we use CORS, we give the Access-Control-Allow-Origin header.
For example, in PHP:
script.php:

<?php
header('Access-Control-Allow-Origin: *');
echo 'Hello!';
?>

Now we can make a request to this script from absolutely any domain (for example, using jQuery):
$(function() {
   $.get('http://example.com/script.php', function(data) {
      alert(data); // Hello!
   });
});

A
Alexey Kureev, 2014-06-27
@xamd

No, but this is "not a bug, but a feature."
You are prohibited by default from processing requests from third-party resources. This allows you to protect yourself from processing requests for processing forms (for example) from the left server. Eliminates various stupid hacking possibilities like brute force, etc.
This will not be fixed because there is nothing to fix.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question