Home>

ScrapboxI want to access the API of the service by GET communication of Ajax, but it does not work due to CORS error.

This articleAccording to Scrapbox, the credential (authentication information) is set in the cookie, so you can get the data by using it.
I got the cookie itself, but I don't know how to pass it in Ajax.
It would be greatly appreciated if you could teach me how to pass credentials with Ajax.

For now, I wrote the following code and tried to execute it, but the communication failed.

const api_url ='***';
$.ajax({
    type: "GET",
    url: api_url,
    xhrFields: {
        withCredentials: true
    }
}).done(function (data, textStatus, jqXHR) {
    console.log("done");
}).fail(function (jqXHR, textStatus, errorThrown){
    console.log("failed");
});
  • Answer # 1

    The method in the referenced article is a method that cannot be used in JavaScript.
    The method of the article is to get the authenticated cookie and embed it in the Python script source.
    In the case of JavaScript, cookies cannot be specified from scripts (reference). WithCredentials: true described in the code of the question sentence specifies that the cookie originally set in the browser is also sent by AJAX, but even if the cookie is set on the browser side, if there is no permission on the API side can not use.
    So, I think this method has no choice but to give up. It may be possible with other methods, but I think it's difficult if it is not an API prepared for JavaScript.