1

I am having a problem with retrieving JSON feeds to use in a widget.

I have googled the hell out of it and just seem to be confusing myself more.

I have this code

function insertReply(content) {
    document.getElementById('holder').innerHTML = content.result;
}

// create script element
var script = document.createElement('script');
// assing src with callback name
script.src = 'https://www.googleapis.com/freebase/v1/text/en/bob_dylan?callback=insertReply';
// insert script to document and load content
document.body.appendChild(script);

from this post - Get JSON data from external URL and display it in a div as plain text

Which works great. However if I change the URL i get no response and no errors in the console.

new URL: http://finance.google.com/finance/info?client=ig&q=NASDAQ:GOOG

Why does one work and not the other?

EDIT @Amit

Amit Sorry for being retarded but I am really new to JQuery and javascript. Where do you put these? I have

<!DOCTYPE html>
<html>
<head>
<title>Widget Holder</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</style>
</head>

<body>
    <form runat="server">
    <div id="holder"></div>
    </form>
    <script type="text/javascript">
        $().ready(function () {
            $.get("http://finance.google.com/finance/info?client=ig&q=NASDAQ:GOOG", function (data) {
                debugger;
                $("#holder").html(data);
            });
        });
    </script>


</body>
</html>

But still get this error

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://finance.google.com/finance/info?client=ig&q=NASDAQ:GOOG. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

Community
  • 1
  • 1
  • **//** [ { "id": "304466804484872" ,"t" : "GOOG" ,"e" : "NASDAQ" ,"l" : "539.79" ,"l_fix" : "539.79" ,"l_cur" : "539.79" ,"s": "2" ,"ltt":"5:04PM EDT" ,"lt" : "May 27, 5:04PM EDT" ,"lt_dts" : "2015-05-27T17:04:30Z" ,"c" : "+7.47" ,"c_fix" : "7.47" ,"cp" : "1.40" ,"cp_fix" : "1.40" ,"ccol" : "chg" ,"pcls_fix" : "532.32" ,"el": "537.01" ,"el_fix": "537.01" ,"el_cur": "537.01" ,"elt" : "May 27, 6:40PM EDT" ,"ec" : "-2.78" ,"ec_fix" : "-2.78" ,"ecp" : "-0.52" ,"ecp_fix" : "-0.52" ,"eccol" : "chr" ,"div" : "" ,"yld" : "" } ] Is this a valid `json`? – Amit Kumar Ghosh May 28 '15 at 09:33
  • What would make it invalid? the //? – Antony Walters May 28 '15 at 09:38
  • but it has `//` appended with it. may be that's causing a problem? – Amit Kumar Ghosh May 28 '15 at 09:39
  • If it is not valid JSON is there anther way to read this feed? – Antony Walters May 28 '15 at 09:42
  • see my answar below.The browser is loading the `json`. – Amit Kumar Ghosh May 28 '15 at 09:48
  • If you paste your JSON [here](http://jsonlint.com/), you will see that is not valid because of the `//`. You have to remove them and then parse it. – JcDenton86 May 28 '15 at 10:18
  • @AmitKumarGhosh Thank you I have edited the original post – Antony Walters May 28 '15 at 10:30
  • @JcDenton86 Thanks for the link. Useful resource. – Antony Walters May 28 '15 at 10:41

2 Answers2

1

I managed to fix the issue using a PHP proxy to fetch the data. The proxy I have used can be found here http://benalman.com/code/projects/php-simple-proxy/examples/simple/

Thank you for all your help on this subject.

0

The script:

$().ready(function () {
            $.get("http://finance.google.com/finance/info?client=ig&q=NASDAQ:GOOG", function (data) {
                debugger;
                $("#holder").html(data);
            });
        });

The html:

<form runat="server">
    <div id="holder"></div>
    </form>

is working for me.

Amit Kumar Ghosh
  • 3,618
  • 1
  • 20
  • 24