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

时间:2013-04-08 10:33:58

标签: php jquery

我学习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方法?

4 个答案:

答案 0 :(得分:1)

发送

标题访问控制 - 允许 - 来源,访问控制 - 允许 - 方法和访问控制 - 允许 - 标题

see here

答案 1 :(得分:1)

@PSRs答案可能会有效,但我所做的只是停止在simpleHTTPServer上运行简单的应用程序并将其移动到mamp服务器,这更好地支持了php ...

答案 2 :(得分:0)

另一种选择是将php解决方案用于简单服务器,而不是python:

php -S localhost:8000

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

sudo apt install php7.2-cli

答案 3 :(得分:-3)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>转移到<head>代码