XMLHttpRequest Cannot Load - Not Allowed By Access-Control-Allow-Origin

2819
2
08-27-2012 03:42 AM
JithenSingh
Esri Contributor
Hi There,

I have an application that communicates to AGOL's Portal API to carry out a few tasks. One of these tasks is to check the membership of a group.

Here is a code snippet below.


function checkMembership() {

    var restService = "https://www.arcgis.com/sharing/community/groups/" + groupId + "/users";
    var restParameters = "?v=1&f=json&token=" + tokenInformation.token;

    $.ajax({
        "type":"POST",
        "url":restService + restParameters,
        "dataType":"json",
        success:function (json) { 
        },
        error:function (xhr, err) {
        }
    })
  }


The error been thrown in the console is:

XMLHttpRequest cannot load https://www.arcgis.com/sharing/community/groups/[GROUP ID]/users?v=1&f=json&token=[TOKEN] Origin http://localhost is not allowed by Access-Control-Allow-Origin.

Now I know this is due to the lack of CORS support on that server and I can see this by the error message and in most cases it can be ignored, but this is stopping the function from being run.

If I can the crafted URL and paste it into a browser, it executes fine.

Any ideas on how one can query the Portal API without this happening?

Thanks
0 Kudos
2 Replies
derekswingley1
Frequent Contributor

Now I know this is due to the lack of CORS support on that server and I can see this by the error message and in most cases it can be ignored, but this is stopping the function from being run.

If I can the crafted URL and paste it into a browser, it executes fine.

Any ideas on how one can query the Portal API without this happening?


Without CORS support, browsers won't let you do a cross-domain POST due to same origin policy. Use a proxy page.
0 Kudos
JithenSingh
Esri Contributor
Thanks Derek. Will give it a go.

Cheers
0 Kudos