lundi 30 août 2021

Can't make POST request because of the CORS problem

Disclaimer: I know this question has been posted a lot of times but even after looking at other topics I still cant figure this out.

I am trying to make a post request to an API using JQuery, with the ajax method, but the browser keeps blocking it due to the Cross Origin policy. I do not own this API and thus I cannot change its code. I have tried multiple solutions, including adding "forbidden headers" to the request.

JQuery code:

$.ajax({
    type: "POST",
    url: "https://vcdn2.space/api/source/" + substring,
    data: payload,
    headers: {
        'origin': 'https://vcdn2.space',
        'sec-fetch-site': 'same-origin',
        'sec-fetch-mode': 'cors',
        'sec-fetch-dest': 'empty',
        'referer': "https://vcdn2.space/v/" + substring
    },
    contentType: "application/x-www-form-urlencoded; charset=utf-8",
    dataType: "json",
    success: function (response) {
        alert("ok1");
    },
    failure: function (response) {
        alert("ok2");
    }

I can make this request without any problems on the server side (using python, Django), but in this case, it needs to be done on the client side. Any ideas?

Thank you in advance




Aucun commentaire:

Enregistrer un commentaire