Error 405 - Cross Domain Request

Asked

Viewed 150 times

0

Guys, I’m making a request for my page on github (tiagosilveiaa.gihub.io) for my test server, just to run the script of the same form, but I was presented a message that the protocols were different (https x http) ""I solved"" by placing the address as follows:

Cod:

//tiagotestes.esy.es/portfolio/php/envia.php

Now I am presented with message 405, which the method is invalid. How can I fix it? Because I can do the POST of my localhost.

Form:

<form id="contato" action="//tiagotestes.esy.es/portfolio/php/envia.php" method="POST">

JS:

$("#enviar").click(function() {
    if (validar()){
        $.post(
                $("#contato").attr("action"),
                $("#contato").serialize()
            )
            .done(function(retorno) {
                alert(retorno);
                $("#nome").val("");
                $("#email").val("");
                $("#telefone").val("");
                $("#mensagem").val("");

            })
            .fail(function(retorno) {
                alert(retorno)
            });
      }
});

PHP:

<?php
header('Access-Control-Allow-Origin: https://tiagosilveiraa.github.io/');
header('Access-Control-Allow-Methods: POST');
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$nome = $_POST["nome"];
$email = $_POST["email"];
$telefone = $_POST["telefone"];
$mensagem = $_POST["mensagem"];

if ($nome ==""){
  echo("Preencha o campo nome");
  die();
} else if ($email ==""){
  echo("Preencha o campo E-Mail");
  die();
} else if ($telefone ==""){
  echo("Preencha o campo Telefone");
  die();
} else if ($mensagem ==""){
  echo("Preencha o campo Mensagem");
  die();
}

$msgfinal = "Oi, você recebeu uma mensagem enviada pelo formulário do Site<br />
                Esses são os dados do cliente:
                <ul>
                <li><h3>Nome: ". $nome. "</h3></li>
                <li><h3>E-Mail: " .$email. "  </h3></li>
                <li><h3>Telefone: ".$telefone." </h3></li>
                </ul>
                <br />
                <p>
                Essa é a mensagem que ele mandou: <br/><br/><blockquote style='border-left: solid 5px #f7ca18; padding-left: 10px;'><i> &nbsp; " .$mensagem.
                "</i></blockquote></p>" ;

// send email
mail("[email protected]","Contato do Site", $msgfinal, $headers);
?>
  • Have you tried: header('Access-Control-Allow-Origin: //tiagosilveiraa.github.io/'); ? Good luck to you!

1 answer

0

Generally 405 indicates preflight/OPTIONS request, where the request is being rejected by the server itself, and does not even reach php or anything else you are using. You have to change apache or . htaccess settings

Browser other questions tagged

You are not signed in. Login or sign up in order to post.