2

I learning ajax, and im setting up a simplehttpserver in a working directory to run some files in the browser. Now what happens is that when I run my html file, that looks like the following:

<!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>

and the 'save.php' is simply this:

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

Then the browser gives me the error:

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

I dont get it why should the post method not be supported?

patriques
  • 5,057
  • 8
  • 38
  • 48

4 Answers4

1

send

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

see here

Community
  • 1
  • 1
PSR
  • 39,804
  • 41
  • 111
  • 151
  • currently I have a Python script that sends headers, how can I make it send all those? So far I have `self.send_header('Access-Control-Allow-Origin', '*')` does this mean I need to have `self.send_header('')` for each header you suggested, on separate lines in my script? Desperate thanks – hello_there_andy Dec 09 '16 at 02:04
1

@PSRs answer might work, but what i did was simply stop running the simple app on the simpleHTTPServer and moved it to a mamp server, that supported php better...

patriques
  • 5,057
  • 8
  • 38
  • 48
1

Another option is to use php solution for simple server rather than the python one:

php -S localhost:8000

If you don't have php installed, run that first:

sudo apt install php7.2-cli
Massyanya
  • 2,844
  • 8
  • 28
  • 37
-3

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

Amir
  • 4,089
  • 4
  • 16
  • 28