"Access-Control-Allow-Origin" Google financial converter gives javascript / jquery error

I work with one project module, where I want to get a converted price from Google finance converter based on the currency code and amount. Here is my code:

$('.actual-price').tooltip({
    content: function(callback) {
        var url = 'https://www.google.com/finance/converter?a=25&from=USD&to=AMD';
        $.get(url, {}, function(data) {            
            callback(data);
        });
    },
    open: function(event, ui) {
        var $id = $(ui.tooltip).attr('id');
        $('div.ui-tooltip').not('#' + $id).remove();
    },
    close: function(event, ui) {
        $('.ui - tooltip').hide();
    }
});

This gives me an error:

XMLHttpRequest cannot load https://www.google.com/finance/converter?a=25&from=USD&to=AED. no The header header Access-Control-Allow-Origin is present in the requested resource. The origin is http://mytestsite.comtherefore not allowed access.

I tried the following ways to solve it, but nothing seems to work for me!

First: Added Access-Control-Allow-Originto web configuration.

<httpProtocol>
    <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
    </customHeaders>
</httpProtocol>

Second: using jsonp data type:

dataType: "jsonp",

And already mentioned the following posts:

No header "Access-Control-Allow-Origin" is present on the requested resource "

"Access-Control-Allow-Origin"

"Access-Control-Allow-Origin" . Origin '...'

"Access-Control-Allow-Origin" "

: localhost, , !

+4
3

. , , (php), , cors-anywhere.herokuapp.com:

var myUrl = 'http://www.geoplugin.net/json.gp?ip=216.58.209.68';

var proxy = 'https://cors-anywhere.herokuapp.com/';

var finalURL = proxy + myUrl;

// With the get JSON (frequently used) method
$.getJSON(finalURL, function( data ) {
    console.log(data);
});

// With the get method
$.get(finalURL, function( data ) {
    console.log(data);
});

// With the post method
$.post(finalURL, function( data ) {
    console.log(data);
});   

.

? , , Google , localhost.

+3

Ajax async: false, :

JavaScript:

$('.actual-price').tooltip({
    content: function(callback) {
        var url = 'https://www.google.com/finance/converter?a=25&from=USD&to=AMD';
        $.ajaxSetup({async: false}); ////////// Added
        $.get(url, {}, function(data) {            
            callback(data);
        });
    },
    open: function(event, ui) {
        var $id = $(ui.tooltip).attr('id');
        $('div.ui-tooltip').not('#' + $id).remove();
    },
    close: function(event, ui) {
        $('.ui - tooltip').hide();
    }
});
0

-, chrome ,

, . , .

0

Source: https://habr.com/ru/post/1674963/


All Articles