Yabada
Yabada

Reputation: 1758

access-control-allow-origin happening randomly

I've got a local server : 127.0.0.1:8000 and a website : 127.0.0.1:9000 both running with play2 framework.

I make multiple jquery ajax request on the server to get json data and load graphics with these datas.

The problem is that in a totaly random way, i get an access-control-allow-origin error. I read a lot about access-control-allow-origin, and modified the server's header response this way :

Response.ok(Engine.executeQuery(identifier, queryParameters))
                .header("Access-Control-Allow-Origin", "*")
                .header("Access-Control-Allow-Methods","GET, POST, OPTIONS")
                .header("Access-Control-Allow-Credentials","true")
                .header("Access-Control-Allow-Headers","Content-Type, *")
                .build();

My ajax calls are made this way:

$.ajax({
        url: obj.url,
        dataType: 'json',
        crossDomain:true,
        success: function (data){
            // load graph
        }

I've 10 ajax calls, all made exactly the same way, at the same target url.

The problem is that when I load the page (and execute the ajax calls), some ajax calls works and some don't. If i refresh the page, a different number of calls works. This look realy random to me.

I also noticed that if I specify async:false to the ajax calls, the error is never fired. Of course, I need the calls to be asynchronous (it would have been too easy).

I saw so many posts about this error that I thought I knew everything about it.

Any help is welcome. Thanks in advance.

Upvotes: 0

Views: 975

Answers (1)

Yabada
Yabada

Reputation: 1758

Problem solved. I refactored the server and it works great. It seems that it was a thread issue, not safe enough.

Thanks for the help.

Upvotes: 1

Related Questions