1

Suppose the following directory structure:

/web
    example.html
    example.js
/example.json

I open example.html with my browser and it runs example.js, which tries to load the data in example.json synchronously using the following call:

$.ajax({url: "file:///example.json",
        dataType: "json",
        async: false,
        success: function(data) {
            console.log(data)
        },
        error: function(request, status, error) {
            console.log(request);
            console.log(status);
            console.log(error);
        }
});

This results in an error with message "Access to restricted URI denied" and error code 1012. So far, I have taken a look at Access to restricted URI denied“ code: ”1012 - Cross domain Ajax request and Access to restricted URI denied code: 1012. The first link suggested that I use jQuery's getJSON method, but this method only works asynchronously. The second link suggests some sort of JSONP callback, but I haven't been able to understand how exactly these work.

Note that this problem easily goes away if I move example.json to /web/example.json, but I would like to avoid this due to some circumstances of my actual problem (what I presented here is a simplification of my actual problem).

EDIT: I am trying this JSONP code, but I am still running into the same error:

$.ajax({url: "file:///example.json",
    dataType: "jsonp",
    success: function(data) {
        console.log(data)
    },
    error: function(request, status, error) {
        console.log(request);
        console.log(status);
        console.log(error);
    }
});
Community
  • 1
  • 1
abw333
  • 5,571
  • 12
  • 41
  • 48
  • Why does it need to be synchronous? Why not just delay execution of specific code until callback function is triggered? – Brad M Jul 25 '13 at 14:50
  • The reason for using AJAX is because it is asynchronous. The reason is lost when you make it synchronous :( – Selvakumar Arumugam Jul 25 '13 at 14:53
  • @Vega The reason I am using AJAX is because it can load my JSON data, not because it is asynchronous. If there is an alternative that can also load my JSON data, I would be happy to hear about it. – abw333 Jul 25 '13 at 14:55
  • @Vega There are scenarios where synchronous ajax is the only solution. – Brad M Jul 25 '13 at 14:57
  • @abw333 The alternative is your web server requesting the JSON data and serving it to the client. – Brad M Jul 25 '13 at 14:58
  • @BradM Making this request synchronous would make my code simpler. I'm actually executing this request inside a for loop and I don't want to do anything until all the requests have fetched their JSON data. Also. I'm not using a web server. This is all local and I would like to keep it that way for simplicity's sake. – abw333 Jul 25 '13 at 14:58
  • in what scenarios is synchronous the only solution? if synchronous is the only solution, you're working in an environment that needs to be updated/replaced. Back on topic, the cross-domain request you're making will not work in chrome regardless of what you do because it's `file://` unless you started chrome with lesser security settings. I suggest working from two domains rather than a domain and the file system if you want to test a cross-domain request. – Kevin B Jul 25 '13 at 15:08
  • Sending ajax requests in a loop while keeping them asynchronous is very easy and maintainable using the promise interface. – Kevin B Jul 25 '13 at 15:11
  • @KevinB Lets say a user closes their browser and you want to send an ajax request during the unload event; synchronous is your only option. – Brad M Jul 25 '13 at 15:12
  • @KevinB When [compiling synchronous C++ operations to JS](http://stackoverflow.com/questions/16281511/is-there-a-way-to-fake-a-synchronous-xhr-request) (e.g., by using [emscripten](https://github.com/kripken/emscripten/)), sometimes you can only use a synchronous operation. That said, I won't necesarily disagree with your assertion that if you're compiling your C++ into JS that your "environment that needs to be updated/replaced". – apsillers Jul 25 '13 at 16:08

3 Answers3

0

By default cross-domain AJAX requests are not allowed. You need permission from the other server using CORS, or you can use JSONP.

Community
  • 1
  • 1
Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
0

Your reasoning for using synchronous ajax is misguided. If you want to wait until all ajax requests complete, use the following methodology.

var ajaxRequest1 = $.ajax();
var ajaxRequest2 = $.ajax();
var ajaxRequest3 = $.ajax();
$.when(ajaxRequest1, ajaxRequest2, ajaxRequest3).done(function(){});

This will be far more efficient/faster than executing a series of synchronous ajax requests inside a loop.

Brad M
  • 7,857
  • 1
  • 23
  • 40
0

Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation.

Look at documentation here.

Ömer Cinbat
  • 400
  • 2
  • 8