jQuery 501错误(不支持的方法('POST'))


jQuery 501 error (Unsupported method ('POST'))

我正在学习ajax,我在工作目录中设置了一个simplehttpserver,以便在浏览器中运行一些文件。现在发生的事情是,当我运行我的html文件时,它看起来像这样:

<!doctype html>
  <html>
    <head>
    <meta charset=utf-8>
    <title>playground</title>
    </head>
    <body>
    <h1>Save</h1>
    <form action="#">
      <textarea name="content" id="content" cols="30" rows="10"></textarea>
      <p><button type="submit">Save</button></p>
    </form>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script>
    (function($) {
      $('form').on('submit', function(e) {
        $.post('save.php', $(this).serialize(), function(data) {
          console.log(data);
        });
        e.preventDefault();
      });
    })(jQuery);
    </script>
    </body>
  </html>

和'save.php'就是这样:

<?php
    $_POST['content'];
?>

然后浏览器给出错误:

POST http://localhost:8000/save.php 501 (Unsupported method ('POST')) in jquery.min.js:4

我不明白为什么post方法不被支持?

send

headers Access-Control-Allow-Origin, Access-Control-Allow-Methods和Access-Control-Allow-Headers

看到

@PSRs的回答可能会起作用,但我所做的只是停止在simpleHTTPServer上运行简单的应用程序,并将其移动到支持php更好的mamp服务器…

另一个选择是使用php解决方案,而不是python解决方案:

php -S localhost:8000

如果您没有安装php,请先运行php:

sudo apt install php7.2-cli

transfer <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> to <head> tag