When it comes to CORS, "incompatibility" and restrictions are something we cannot leave aside.
An alternative way is to use a php file as an intermediary, playing a proxy role.
You can use CURL:
<?php
//Proxy.php
$curl = curl_init();
$url = 'http://www.dominioexterno.com.br/acao/';
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,image/jpeg,image/jpg,image/gif,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: pt-BR,en-US,en,pt;q=0.5";
$header[] = "Pragma: ";
curl_setopt($curl, CURLOPT_URL, $url );
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, array('param' => 'value'));
$reponse = curl_exec( $curl );
curl_close( $curl );
echo $reponse;
curl_setopt($curl, CURLOPT_POST, TRUE);
Here the request was set to POST
curl_setopt($curl, CURLOPT_POSTFIELDS, array('param' => 'value'));
.
and here are the parameters param=value
For CORS to work, the server also needs to be configured to accept the headers that JS will send.
– bfavaretto
This solution depends on many preconditions. The best way to do this is to create an entry on the server of the application that makes this request (via
curl
or similar) and returns the data.– Henrique Barcelos