Calling web api from javascript always leads to failure

function opening(event, nodes, node) { alert("opening"); $.ajax({ url: 'https://permid.org/1-5000617737?format=json-ld', type: "GET", contentType: "application/json; charset=utf-8", dataType: "json", success: function (data, textStatus, xhr) { alert('success'); }, error: function (xhr,textStatus, errorThrown) { alert("failed") } }); }

This function always returns failed alert.

I want to access permId data from my javascript/ajax code.

Best Answer

  • zoya faberov
    Answer ✓

    Hello @priyanka.srivastava,

    You are likely running into CORS

    I.e. the origin that you are at, is not on the list of the allowed origins for permid.org domain.

    You can test this with the html test page below.

    If you are able to run the page on your webserver, clcik request and get back the entity, your origin is allowed.

    If not- you can re-test with chrome with web security disabled, as any origin will be allowed

    chrome.exe --user-data-dir="C:/Chrome dev session"--disable-web-security

    discussed in detail here

    here

    test page:

    <!DOCTYPE html>
    <html>
    <head></head>
    <body>
    <h1>The XMLHttpRequest Object</h1>
    <button type="button" onclick="loadDoc()">Request data</button>
    <p>Click the button several times to see if the time changes, or if the file is cached.</p>
    <p id="demo"></p>


    <script>
    function loadDoc() {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange=function() {
    if (this.readyState == 4 && this.status == 200) {
    document.getElementById("demo").innerHTML = this.responseText;
    }
    };
    xhttp.open("GET", "https://permid.org/1-4295861160?format=turtle&access-token=YOUR_VALID_TOKEN_HERE", true);
    xhttp.send();
    }
    </script>

    </body>
    </html>

Answers