0

I tried to follow several tutorials to make an AJAX post request, but whenever I try to do it, it always returns this error: POST http://127.0.0.1:5500 405 (Method Not Allowed)

I tried to make a POST request to a PHP file and it also failed, although the instructor's request worked.

This is code supposed to return a message that greeting the user and tells him when was his last login.

My ajax request

$(function () {
  $("button").on("click", function () {

    $.post("auth.php", { name: "John", lastlogin: "Yesterday" },function (data, status, xhr) {
        console.log(data); // Supposed to return php file execution.
      });
  });
});

PHP code in auth.php file

<?php

  if (isset($_POST['name'])) {
    echo 'Hello, ' . $_POST['name'] . ' Your last login at ' . $_POST['lastlogin'];
  }

?>

It returns the 405 method not allowed error.

And when I try to make a get request the response text comes as the PHP code, but in the tutorial, the parameter or the response text returns:

Hello, John Your last login at Today.

Notes that may or may not help:

  • I'm using the Live Server extension, it uses port 5500.

  • I don't have knowledge of PHP yet, just practicing Ajax requests before moving to the backend path using php.

  • I'm using Linux (Ubuntu) OS.

  • index.html path Javascript\index.html.

  • javascript file path Javascript\scripts\main.js.

  • auth.php path Javascript\auth.php.

Thanks a lot to anyone who read all of this, I appreciate your seeking to help.

Ahmed Osman
  • 56
  • 1
  • 5
  • 3
    "I don't have knowledge of PHP yet, just practicing Ajax requests before moving to the backend path." — It generally helps to have a working backend before you try making requests to it. – Quentin Sep 07 '21 at 17:13

0 Answers0