0

This is the one I am testing with, specifically:

static json data

I have run into many cross-site request errors, though I don't see how this should be different than requesting an image that is statically hosted on the same site, which I can easily load into my test html file.

I have tried the following:

$.getJSON('http://anyorigin.com/get?url=maxcandocia.com/static/blog/test_output3.json&callback=?',  
function(data){
    $('#output').html(data.contents);
});

var network_data

$.getJSON("http://maxcandocia.com/static/blog/test_output3.json", 
function(data){
    network_data = data.contents;
})
Max Candocia
  • 4,294
  • 35
  • 58
  • I tried getting around the origin using this service, but it returns a 404 error. $.getJSON('http://anyorigin.com/get?url=maxcandocia.com/static/blog/test_output3.json&callback=?', function(data){ $('#output').html(data.contents); }); – Max Candocia Nov 11 '16 at 02:11
  • Edit: I put a couple of attempts that I made up in the original post. I've also tried setting document.domain, but I can't do that due to security restricitons. My main issue is that I am not sure why my browser would not be able to retrieve arbitrary data that is not executed immediately, when it can already retrieve image data from the server that is hosted the exact same way. – Max Candocia Nov 11 '16 at 02:18

2 Answers2

0

In general, you can't access a resource on another origin (in essence domain-port combination) through JavaScript unless that origin explicitly allows you to through the use of specific headers, specifically the Access-Control-Allow-Origin.

It seems that anyorigin.com, the service that you're using, for some reason fails to redirect correctly; this does not appear to be your fault, but something wrong with the service. I would recommend that you try some other, equivalent service, such as https://crossorigin.me/ (just add https://crossorigin.me/ in front of the URL):

var network_data;
$.getJSON("https://crossorigin.me/https://maxcandocia.com/static/blog/test_output3.json", 
function(data){
    network_data = data.contents;
})

If you control the server yourself, it would be better to just set up the server to send the Access-Control-Allow-Origin header for your JSON file.

Frxstrem
  • 38,761
  • 9
  • 79
  • 119
-1

Access-Control-Allow-Origin is not an issue with your code. It means you have an issue with security policies.

If you own maxcandocia.com you need to adjust the headers to allow your origin (where you're running the script). If not, you have no chance here unless they change their policies for you (unlikely.

You would instead be looking for a server-side solution, such as what Frxstrem has suggested. Or if you own the script send back this header with the origins you require:

Access-Control-Allow-Origin: http://yourorigin.com http://maxcandocia.com https://maxcandocia.com

I have an in depth answer here on using json with jquery: https://stackoverflow.com/a/17768384/2376468

Community
  • 1
  • 1
Mark Hughes
  • 418
  • 3
  • 16
  • I have tried this, and I receive the error: `XMLHttpRequest cannot load http://maxcandocia.com/static/blog/test_output3.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.` – Max Candocia Nov 11 '16 at 02:20
  • Access-Control-Allow-Origin is not an issue with your code. It means you have an issue with security policies. Do you own maxcandocia.com? If not, you have no chance here unless they change their policies or allowing your origin (where ever you're running your code from). You would be looking for a server-side solution, such as what Frxstrem has suggested, or your own built solution. I have updated my answer to reflect this – Mark Hughes Nov 11 '16 at 03:10