Het Delwadiya
Het Delwadiya

Reputation: 119

POST request is working fine with postman but not in website

export const executeCode = (data) => {
       return fetch("https://ide.geeksforgeeks.org/main.php",{
              method: "POST",
              headers: {
                     Accept : "application/json",
                     "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
              },
              body: data
       })
       .then(response => response.json())
       .catch(err => console.log(err))
}

I'm sending a POST request to G4G's compiler API. It's working fine in postman but not working in my react app.

Error msg (image)

working in postman (image)

Upvotes: 0

Views: 6900

Answers (3)

kashan ilyas
kashan ilyas

Reputation: 352

Add this line of code on backend in .htacess file

<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "POST,GET,OPTIONS,DELETE,PUT"
Header set Access-Control-Allow-Headers "*"
</IfModule>

Upvotes: 1

mmantach
mmantach

Reputation: 148

Please add to the header of the request Access-Control-Allow-Origin: * let me know if it's working

Upvotes: 1

Radu Diță
Radu Diță

Reputation: 14171

When making a call from an website there are security considerations to be taken into account. One of them is CORS.

In a nut shell the browser asks the server if it allows HTTP calls (calls that are considered not simple actually).

Postman works because it doesn't do this check.

G4G need to respond to OPTIONS requests with a proper CORS response that allows your host to call them.

Upvotes: 1

Related Questions