I need load external resources from another server like css, template, data… but I don’t know what is the better way for do it.
Naturally when we load external file there is the Access-Control-Allow-Origin problem.
So, the solutions are:
– jsonp
– allow transfer with proxy.php, like here:
// If I want style.css the url for ajax call is /proxy.php?file=style.css
header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']);
echo file_get_contents($_GET['file']);
How we behave in these cases?
CSS can be transfered cross-domain using a
<link>tag which you can generate in javascript and append to<head>, scripts can be appended to the<head>just like css or loaded using jquery’s$.getScript, and data can be transferred usingjsonpwith jquery’s$.ajaxor$.getJSON.Any other forms of data will require a server-side proxy.
I personally prefer to use
jsonpwhen transferring data, however when that is not possible, the only other choice is a server-side proxy.