0

I'm having issues loading an array from a file from $.get(). I get a good HTTP status code so the file is accessed, but it's not turning it into an array.

Here's the code I have

$(function() {

    $.get('https://gist.github.com/storbeck/6956620/raw/e274c8af60ef25e7bc481fded0c9e4e8e8412e75/testdata', function(data) {
        $('#container').highcharts('StockChart', {      

            rangeSelector : {
                selected : 1
            },

            title : {
                text : 'Test Data'
            },

            series : [{
                name : 'Data',
                data : data,
                tooltip: {
                    valueDecimals: 2
                }
            }]
        });
    });
});

Here's a jsFiddle of the current code of it not working.

I was able to get it working by copy/pasting the array into the data variable and not using $.get so I don't think the array is the problem. Here's that version

One other thing I'm having issues with is the epoch time in Highcharts, when I test the time in an online convertor, it converts the time just fine.

3 Answers 3

2

You are getting an empty result because the page you are trying to load has Access-Control-Allow-Origin header that limits who can remotely load the page. Neither $.ajax, $.post, $.get will load this page because they are blocked.

If you load your script into your local browser you will see in your browser console log:

XMLHttpRequest cannot load
https://gist.github.com/storbeck/6956620/raw/e274c8af60ef25e7bc481fded0c9e4e8e8412e75/testdata.
Origin http://www.yourdomain.com is not allowed by Access-Control-Allow-Origin. 
Sign up to request clarification or add additional context in comments.

Comments

0

its a Same-Origin-Policy problem...

If you have access to the file, that generates the data, you could use JSONP, the data should be formatted with a callback function. If you have access to the server you could add something like this:

Access-Control-Allow-Origin: ...

But the only way that might work for you, is to get the result first with a PHP, ASP or whatever script, and then call that script with your ajax. But this should be on the same domain ofc.

Hope it helps.

Comments

0

This is because of Same Origin Policy

One way you could work around this is by requesting the file on your server, where there aren't issues about this, and then making the ajax GET call to your server:

Create a new page on your server, and request the gist file in that page and return its content as the response. Of course, you need dynamic pages for this to work

Change the endpoint in your jQuery $.get to that page, for example: www.mydomain.com/highchartvalues

Profit

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.