6

I'm working with one project module, where I want to get converted price from Google finance converter based on from 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();
    }
});

It gives me an error:

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

I've tried following ways to solve it, but seems like nothing works for me!

First: Added Access-Control-Allow-Origin to the web config.

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

Second: Using datatype jsonp:

dataType: "jsonp",

And already referred following posts:

“No 'Access-Control-Allow-Origin' header is present on the requested resource”

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

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '…' is therefore not allowed access

“No 'Access-Control-Allow-Origin' header is present on the requested resource”

Side: It's working with my localhost, but when I try with another domain, it's give me an error!

Community
  • 1
  • 1
Hina Khuman
  • 757
  • 3
  • 14
  • 41
  • You are calling https://www.google.com/finance/converter?a=25&from=USD&to=AMD this url but this url not return anything they display html form so you cannot call using $.get – Paresh Gami Apr 17 '17 at 11:19
  • @PareshGami: Then why it is working with my localhost? – Hina Khuman Apr 17 '17 at 11:26
  • if you put it in live server that is working you mean? – Paresh Gami Apr 17 '17 at 11:28
  • @PareshGami: *It's working with my localhost, but when I try with another domain, it's give me an error!* – Hina Khuman Apr 17 '17 at 11:34
  • 1
    What happen when you install this to your browser? https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi – Samuel Tulach Apr 19 '17 at 14:12
  • @SamuelTulach: Thanks! It works on chrome, but I'm developing a product(plugin) for end users, wherein I can't ask end users to install specific browser and extension! – Hina Khuman Apr 20 '17 at 05:12
  • https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS you could take a look at this, if it still isn't solved yet. – King Reload Apr 25 '17 at 09:00

3 Answers3

4

Allow access origin must be allowed also at side, when you sending requests. If not you can install some browser extensions to bypass it or download entire page (php) end then process it, but you can also try 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);
});   

More here.

And why is your app working on localhost host? I thing it is because Google have access origin set to allow connection from their domains and localhost.

Samuel Tulach
  • 1,319
  • 13
  • 38
0

You need to change Ajax Setting async: false as below:

JavaScript Code:

$('.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();
    }
});
Sharad Kale
  • 971
  • 1
  • 7
  • 19
0

This is an error when you try to make requests to another servers from local without a web server, if you are using chrome on local, just add this to your browser

when you upload the app to the server in production will work normally. because that's just a local problem that the browser block that types of requests.

xploshioOn
  • 4,035
  • 2
  • 28
  • 37
  • I'm developing a product(plugin) for end users, wheres I can't ask end users to install specific browser and extension! – Hina Khuman Apr 24 '17 at 05:06
  • as I said, this will happen just on your local on development, when the app is on production, this wouldn't happen – xploshioOn Apr 24 '17 at 12:13
  • Then that is not a solution!! see the comment by Samuel, just under the question. – Hina Khuman Apr 24 '17 at 12:16
  • and that is not a problem for you anymore, really, that is just a problem on development mode and you can install an extension or put your app in a web server that let you make cross domain request. that in local is not neccesary so you install a plugin to solve this on development, but in production mode, when you deploy the app, that is not a problem because don't happen. I was in the same with ruby on rails and that's the solution. don't keep wasting time. try starting your app on production mode and see if the error is there, it wouldn't be. – xploshioOn Apr 24 '17 at 12:38
  • another option is to use jsonp with jquery but as I said, that is not necesary. have a good day my friend – xploshioOn Apr 24 '17 at 12:44
  • Okay, I've tried with jsonp but it didn't work, let me give some time to run on production, then will let you know, thank you so much for detailed response :) – Hina Khuman Apr 24 '17 at 12:52
  • I've tested on my live production server, error comes there too! – Hina Khuman Apr 26 '17 at 07:04