简体   繁体   中英

405 method not found. On localhost

So I'm making a form that puts info into my local mysql db. But I stuck when I try to POST it. I getting "405 method not found" when try to debbug. I'm sing xampp for my virtual DB, maybe it's because of that? The code:

HTML:

<!DOCTYPE html>
<html>
<head>
  <title>Kasmetinių atostogų prašymas</title>
  <link rel="stylesheet" type="text/css" href="style.css" />
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
    <div class="title">
      <h1>Kasmetinių atostogų prašymas</h1>
    </div>
    <div class="form">
  <form id="requestForm" method="POST"  target="_blank">

    <input type="date" name="request_date" id="input_field" placeholder="Prašymo data"  required></br></br>

    <input type="text" name="first_name" placeholder="Vardas" id="input_field" required></br></br>

    <input type="text" name="last_name" placeholder="Pavardė" id="input_field" ></br></br>

    <input type="number" name="personal_code" placeholder="Asmens kodas" id="input_field" min="11" max="11" ></br></br>

    <input type="text" name="p_address" placeholder="Jūsų adresas" id="input_field" ></br></br>

    <input type="date" name="requestDateFrom" id="input_field" placeholder="Atostogos nuo"  ></br></br>

    <input type="date" name="requestDateTo" id="input_field" placeholder="Atostogos iki"  ></br></br>

    <input type="number" name="daysNumber" placeholder="Atostogų dienų skaičius" id="input_field" ></br></br>
  </br>
  <Input type="button" name="submit_button" id="buttonLast" value="Patvirtinti">
  </form>
</div>  
<script>
  $(document).ready(function(){
    $("#buttonLast").click(function(){
      $.ajax({
        url:"http://127.0.0.1:5500/insert.php",
        type: "POST", 
        data:$("#requestForm").serialize(),
        success:function(response)
        {
          alert("Well done!");
        }
      });
    });
  });
</script>
</body>
</html>

And this is php code to connect db and post info into specific columns. For the purpose of test I trying to post just from the 3 cols. PHP:

<?php



$con = mysqli_connect("localhost","root","","test");

if(!$con)
{
  echo 'Connection problems';
}
else
{
  echo 'Ok';
}


if(isset($_POST['submit'])){
$date = $_POST['requestDate'];
$name=$_POST['firstName'];
$lname = $_POST['lastName'];


$query = "insert into info (date,name,lname) values ('$date','$name','$lname')";


if($con->query($query) === true )
{
  echo 'Duomenys išsaugoti!';
}
else{
  echo 'Duomenų nepavyko išsaugoti!';
}
}
header("refresh:2; url=index.html");
?>

Change

type: "POST"

to

method:"POST"

Other error you might encounter:

You are using requestDate in php but request_date in html. Similar for other params.

Update: Add cors header to Ajax call

url:"http://127.0.0.1:5500/insert.php",
method: "POST", 
headers: {
   'Access-Control-Allow-Origin': '*'
},
data:$("#requestForm").serialize(),
success:function(response)
{
  alert("Well done!");
}
$(document).on("submit", "#requestForm", function (event) {$.ajax({
    url:"https://127.0.0.1:5500/insert.php",
    type: "POST", 
    data:$(this).serialize(),
    success:function(response)
    {
      alert("Well done!");
    }
  });
});

Try this Jquery Code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM