D
D
DamWolf2020-09-30 14:18:37
CORS
DamWolf, 2020-09-30 14:18:37

How to send a POST request to the form of someone else's site and get a response?

Task: It is necessary to send a post request to someone else's site with a specific id number, so that I get a response with an answer whether this id is suitable.

I did it in 2 ways.

I tried to do it via fetch(). At the beginning I had a header - content-type: application/json. But I had an error and required Access-Control-Allow-Origin.
But now I am getting the following error:

The provided value is not of type "(sequence<sequence<ByteString>> or record<ByteString, ByteString>)".


let post_id = "<?php echo $_POST['id'] ?>";
let url = "https:";
const data = {"id" : post_id};
console.log("data id: " + data);
    
fetch(url, {
        method: 'POST',
        headers: "Access-Control-Allow-Origin",
        body: JSON.stringify(data)
    }).then((value =>
    console.log(value)
        ));
    console.log("post_id: " + post_id);
    console.log("url: " + url);


I did the same through XMLhttprequest.

Gives an error message. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin'

let url = "https:";
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader("authorization", "Token xxxxxx");
xhr.setRequestHeader("Access-Control-Allow-Origin", "1111");
xhr.send();
xhr.responseText;


Help me please!!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Kylin, 2020-09-30
@palkan_karabov

For security reasons, requests from foreign domains are not accepted, so you need to allow them to be accepted.

header("Access-Control-Allow-Origin: http://example.com");

Google CORS

R
Rsa97, 2020-09-30
@Rsa97

Make a request to the script on your site so that it makes a request to a third-party resource. CORS protection is now available in all browsers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question