Enable 'Access-Control-Allow-Origin' in jQuery [xml]

1

I do not have access to the .htaccess server and need to enable cors in jQuery

The code to access the WebService is:

$(document).ready(function(){
    jQuery.support.cors = true;
    $.ajax({
        url: 'https://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote',
        crossDomain:true,
        dataType: 'text/xml',
        success: function(response){
            console.log(response);
        }
    });
});

The request headers are:

Request Headers
:authority:finance.yahoo.com
:method:GET
:path:/webservice/v1/symbols/allcurrencies/quote
:scheme:https
accept:*/*
accept-encoding:gzip, deflate, br
accept-language:pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4
origin:http://localhost
referer:http://localhost/biblioteca/cotacao/jQueryYahooFinance/
user-agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/59.0.3071.109 Chrome/59.0.3071.109 Safari/537.36

In .htaccess I would activate using:

Header set Access-Control-Allow-Origin "*"

In php I would activate using:

<?php header('Access-Control-Allow-Origin: *'); ?>
    
asked by anonymous 09.08.2017 / 18:04

3 answers

1

The Access-Control-Allow-Origin can only be enabled on the server. The purpose of this rule is for the browser not to be able to access content that the server does not want to be accessed when the request domain is not the same as that of the server.

    
09.08.2017 / 18:06
1

O should be added to the .xml server (which is not possible because it is the Yahoo server, only they have this control) and no on your page, if it could be done directly this by the client would be a security flaw and would not even need CORs control.

However it is possible to create a web-proxy with curl to work around this, an example:

Create a file named moedas.php and add this:

<?php

$url = 'https://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

//Define um User-agent
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:21.0) Gecko/20100101 Firefox/21.0');

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);

//Retorna a resposta
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//Resposta
$data = curl_exec($ch);

if($data === false) {
    echo 'Erro ao executar o CURL: ' . curl_error($ch);
} else {
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if ($httpcode !== 200) {
        header('X-PHP-Response-Code: ' . $httpcode, true, $httpcode);
        die('Erro ao requisitar o servidor');
    }
}

header('Content-Type: text/xml');

//Exibe dados
echo $data;

Then in Ajax call:

$(document).ready(function(){
    $.ajax({
        url: 'moedas.php',
        success: function(response){
            console.log(response);
        },
        error: function(err){
            console.log(err);
        }
    });
});
  

Note: If you are using PHP 5.4+ you can switch header('X-PHP-Response-Code: ' . $httpcode, true, $httpcode); to http_response_code($httpcode);

    
09.08.2017 / 18:16
0

If you create a php file and do:

<?php
    call_user_func($_POST['funcao']);
    function cotacao(){
        /* Faço load do arquivo xml */
        $xml = simplexml_load_file('https://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote');
        /* Retono uma String xml bem formada */
    echo $xml->asXML();
    }
?>

And call this document:

$(document).ready(function(){
    $.ajax({
        url: 'documento.php',
        dataType: 'text/xml',
        type: 'post',
        success: function(response){
            console.log(response);
        }
    });
});

It works great, I wanted to do this direct call through jQuery, but for what I understand it has no way.

    
09.08.2017 / 19:31