0

I'm trying to get headlines from yahoo finance and the following code doesn't seem to work. It goes straight to the error handler. I tried a few other URL's and they don't seem to work either. I tested this code within the android emulator with ionic. What am I doing wrong?

$http.get("http://feeds.finance.yahoo.com/rss/2.0/headline?s=GOOG&region=US&lang=en-US")
    .success(function(data) {
        $log.log(data);
    })
    .error(function(data) {
        $log.log("ERROR: " + data);
});      

UPDATE: Read through Mitch's links, you'll very likely find a solution there like I did. For me I had to use the --disable-web-security when starting chromium. Apparently the allow origin plugin isn't enough for these types of http request's.

Also, if you're having trouble in the emulator don't use live reload. That seems to cause a problem with http requests.

AppTest
  • 425
  • 5
  • 19

1 Answers1

0

If you look at your developer console, you will see this message : No 'Access-Control-Allow-Origin' header is present on the requested resource.

It means that the response received does allow requests from localhost, so your browser blocks it. Javascript is limited by the "same origin policy" for security reasons so that a malicious script cannot contact a remote server and send sensitive data. More details on this kind of problem here: https://stackoverflow.com/a/9311585/2298988

This only happens because you're testing your application in a browser though. If you build your Ionic app and test it on a device, it should work perfectly.

To test your app in a browser, you have to disable web security. Which you should do only for development purposes, never when surfing.

Here is a post explaining how to disable web security in Chrome: Disable same origin policy in Chrome

It seems there is a plugin doing it for Firefox, but I haven't tried it: Disable firefox same origin policy

Community
  • 1
  • 1
Mitch
  • 581
  • 5
  • 7
  • Thank you. I thought I had this covered when I used the same origin extension but apparently I still needed to include the the flag --disable-web-security on startup. – AppTest Nov 10 '15 at 16:08
  • Also you say if I build it and test on a device it'll work. But does that mean it can't work in an emulator? – AppTest Nov 10 '15 at 16:09
  • It should work in an emulator as well. This security comes from the browser. On Cordova, it's handled by the whitelist plugin https://github.com/apache/cordova-plugin-whitelist – Mitch Nov 10 '15 at 16:11
  • I already had the whitelist plugin installed but it appears to be a problem with livereload in the emulator. If you turn livereload off it seems to work. Thank's again. – AppTest Nov 10 '15 at 22:00