2

I'm having problems calling php functions from javascript. I have tried some of the suggestions on stackoverflow without any success. I'm not sure what I'm missing here...

I'm writing to submit a simple POST request to my php file, but I get this error in my browser console:

POST http://localhost:8000/myphp.php 501 (Unsupported method ('POST')) 

I'm writing a single page web app that is currently running on my local machine with a simple server running at /projecthome/

 python -m SimpleHTTPServer

My files are laid out like this:

/projecthome/index.html
/projecthome/myphp.php
/projecthome/js/myjs.js

index.html:

...
<script src="js/myjs.js" type="text/javascript"></script>
<script>
    $(function() {
        run();
    });
</script>
...

myjs.js:

function schemaCreationTool() {
    var id='someID';

    $.ajax({
        url: 'myphp.php',
        type: 'POST',
        data: {id:id},
        success: function(data) {
            console.log(data);
        }
    });
}

myphp.php:

<?php

if (isset($_POST['id'])) {
    select($_POST['id']);
}

function select($x) {
    echo "The select function is called.";
}
}

I followed solutions from these posts:

calling a php function in javascript

Call php function from javascript and send parameter

4
  • What's in redirect.php? Commented Oct 24, 2014 at 19:13
  • What is running on port 8000? Commented Oct 24, 2014 at 19:15
  • 4
    This has nothing to do with PHP or Javascript. You're running Python's SimpleHTTPServer, which doesn't support POST requests. Switch to a web server that does. Commented Oct 24, 2014 at 19:15
  • redirect.php is actually myphp.php (edited all of my file names for this post) sorry for the confusion. It's been corrected. localhost:8000 is running a simpleHTTPServer, which loads index.html Commented Oct 24, 2014 at 19:24

1 Answer 1

5

Python's SimpleHTTPServer doesn't natively support POSTs. You can extended it to do so with this little handler:

http://georgik.sinusgear.com/2011/01/07/how-to-dump-post-request-with-python/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.