1

I am trying to send discord webhook message from my PHP website

I see on the console

This error dont read(" I am trying to send diI am trying to send discord webhook message from my PHP website I am trying to send discord webhook message from my PHP website scord webhook message from my PHP website")

(in the localhost the code works, but in the host not working)

I tried to solve it for days

Please I need help

my code

<?php
header('Access-Control-Allow-Origin: ');

?>
<?php include 'header.php' ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="assets/css/main.css?">
    <title>title</title>
</head>
<body>
    <h1 class="msg">welcome, we have send you the details in the provided webhook!</h1>
    <script>
    let webh = "https://discord.com/api/webhooks/xxx/xxx";
    function send() {
    var url = webh
    var request = new XMLHttpRequest();
    request.open("POST", url);
    request.setRequestHeader('Content-type', 'application/json');
        
    var myEmbed2 = {
        "author": {
            "name": "username",
            "url": "https://i.imgur.com/pic.jpg",
        },
        "title": "title",
        "url": "https://google.com/",
        "description": "title",
        "color": 15258703,
        "fields": [
            {
                "name": "text",
                "value": "text",
                "inline": true
            },
            {
                "name": "text",
                "value": "text",
                "inline": true
            },
            {
                "name": "text",
                "value": "text"
            },
            {
                "name": "Thank You!",
                "value": "text"
            }
        ],
        "thumbnail": {
            "url": "https://i.imgur.com/pic.jpg"
        },
        "footer": {
            "text": "text",
            "icon_url": "https://i.imgur.com/pic.jpg"
        }
    }
    
    var params = {
        username: "username",
        avatar_url: "https://i.imgur.com/pic.jpg",
        embeds: [ myEmbed2 ]
    }
    
    request.send(JSON.stringify(params));  
} // end send
send()
    </script>
  
</body>
</html>
Mic12zz
  • 21
  • 4
  • You cannot use Javascript to perform a post to a different server you don't own - since the JS is sent from the client computer... however you can do the post in php file on your server you post from JS to a php file on the server - post needed variables to that file and from that php file do your call to the next server receive response and send it back to your JS code. – Shlomtzion Sep 12 '21 at 15:29
  • Actually the post is sent to the other server but the response is blocked. – Shlomtzion Sep 12 '21 at 15:30
  • Does this answer your question? [Why does my JavaScript code receive a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error, while Postman does not?](https://stackoverflow.com/questions/20035101/why-does-my-javascript-code-receive-a-no-access-control-allow-origin-header-i) – Alon Eitan Sep 12 '21 at 15:40
  • Also you won't be able to pass anything like that ... change application/json to application/x-www-form-urlencoded and your json will be passed in the raw body. – Shlomtzion Sep 12 '21 at 15:46
  • BTW, I know you didn't ask about postman, but that question will give you a good explanation about CORS restrictions, and why your approach will not work unfortunately – Alon Eitan Sep 12 '21 at 16:00

0 Answers0