0

I have a website served up using Nginx. I've create a very simple web-page with a p tag to display the contents of a file, test.html. I have two buttons, one that does a GET request using $.ajax, and one that does a POST request using $.post.

The GET request works fine, and the contents of the file test.html display in my p tag. When I try to POST to that same file, however, I get an error in the console: "Failed to load resource: the server responded with a status of 405 (Not Allowed)". The POST request is pretty simple, taken right from the example on W3Schools.com - https://www.w3schools.com/JQuery/jquery_ajax_get_post.asp. So I am baffled.

I tried to read and understand what a 405 error could mean. Presumably it means that the POST request is not supported by this URL. But how would I enable it to be supported?

    <p id="content-from-ajax"></p>

    <button id="get-content-btn">Get Content</button>

    <button id="post-something-btn">Post something</button>

    <script type="text/javascript">
        $("#get-content-btn").click(function() {
            $.ajax({type: "GET", 
                    url: "test.html", 
                    success: function(result) {
                        $("#content-from-ajax").html(result);
                        alert("GET successful");
                    }
                   });
        });

        $("#post-something-btn").click(function(){
            alert("GRRRR");
            $.post("test.html",
                   {
                name: "Donald Duck",
                city: "Duckburg"
            },
                   function(data, status){
                alert("something worked");
            });
        });
    </script>
S. Harper
  • 127
  • 8
  • A GET request can be used to retrieve assets (as you are doing), however a POST request can only be sent to a server – ic3b3rg Jan 04 '19 at 22:29
  • https://stackoverflow.com/a/3477374/753237 might be helpful – ic3b3rg Jan 04 '19 at 22:30
  • Both this page, and the html file i am attempting to POST to are hosted on an Nginx server, and both are on the same domain. Plus, I thought AJAX only runs properly if it's used on a server? – S. Harper Jan 04 '19 at 22:32
  • 1
    you're trying to POST to an `html` file - you need to POST to an application server - Nginx is a web server and a reverse proxy – ic3b3rg Jan 04 '19 at 22:37
  • About the status code 405: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405 – FZs Jan 04 '19 at 22:37
  • Another link that might help: https://stackoverflow.com/questions/936197/what-is-the-difference-between-application-server-and-web-server – ic3b3rg Jan 04 '19 at 22:38
  • Ah! I do know a tiny (miniscule) amount of Ruby on Rails. So i'm vaguely familiar with the use of an Application Server (i use Passenger). So what you're saying is, in a pure HTML/CSS/Javascript website, there is no way of handling POST requests? – S. Harper Jan 04 '19 at 22:41
  • (Still reading the suggested links) – S. Harper Jan 04 '19 at 22:42
  • Ok, so it sounds like from what I read on https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9, the logic for what happens when a POST request comes in is determined by the server which I guess goes back to what you were saying @ic3b3rg about needing an Application Server. Which I guess means i'm trying to do something silly that can't be done using Nginx alone? – S. Harper Jan 04 '19 at 22:58
  • That is correct – ic3b3rg Jan 04 '19 at 22:58
  • Awesome. At least I don't need to chase my tail anymore. Haha. Thanks for your time. – S. Harper Jan 04 '19 at 22:59

1 Answers1

0

For a POST request to access resources hosted on your web server you will need an application server. Examples include Laravel for PHP, Spring for Java, and Node for JavaScript.

Many application server require you to explicitly specify what type of request a particular endpoint can receive, this can be confusing when learning a new web application framework because a GET request is often the default.

Though a POST request must be handled by an application server it doesn't need to be one your hosting. So you can access public APIs with a POST request (depending on the API and the endpoint your using) without hosting your site on an application server. So if this project is purely educational, this is the best way to test using a POST request without going through the trouble of configuring one yourself.