Received POST functions from JS

#1

Hello. Mongoose is new to me, and I’m trying to learn it bit by bit, but I came across a problem stated below.

  1. My goal is: To receive user input data from webpage then print it in console.
  2. My actions are: I used the simplest_web_server then added a few bits of code such as
mg_register_http_endpoint(nc,"/post_ID",handle_post_ID)

then used the sample for the code above:

static void handle_post_ID(struct mg_connection *nc, int ev, void *ev_data) {
  (void) ev; (void) ev_data;
  mg_printf(nc, "HTTP/1.0 200 OK\r\n\r\n[I am Hello1]");
 nc->flags |= MG_F_SEND_AND_CLOSE;
}

just to see how it really works.

for my JS code:

var myUrl = "/post_ID",                
    devID = '',
    inputID = $('#inputID'),
    outputID = $('#outputID');

$(document).on('submit', function (e) {

    e.preventDefault();
    devID = inputID.val();                          // get the value of input

    $.ajax({
        type: "POST",
        url: myUrl,
        data: devID,
        processData: false,
        contentType: false,
        cache: false,
        success: function (d) {
            console.log("Data: "+d);
            outputID.html(devID);                   // display value of devID
        }
    });
    
});
  1. The result I see is:

POST http://localhost:8053/post_ID 404 (Not Found)
send @ jquery.min.js:2
ajax @ jquery.min.js:2
(anonymous) @ jqmain.js:11
dispatch @ jquery.min.js:2
v.handle @ jquery.min.js:2

I was expecting to see the message of the handler but it says 404 not found.
I don’t know the cause of this problem as I’m currently learning about mongoose.
Also I only attached the header and source files of mongoose as stated in their instructions on Github.

Any help or clue is greatly appreciated.
Thank you.

#2

It works for me with the simplest_web_server example.
C code:

static void handle_post_ID(struct mg_connection *nc, int ev, void *ev_data) {
  (void)ev;
  (void)ev_data;
  mg_printf(nc, "HTTP/1.0 200 OK\r\n\r\n[I am Hello1]");
  nc->flags |= MG_F_SEND_AND_CLOSE;
}

int main(void) {
  struct mg_mgr mgr;
  struct mg_connection *nc;

  mg_mgr_init(&mgr, NULL);
  printf("Starting web server on port %s\n", s_http_port);
  nc = mg_bind(&mgr, s_http_port, ev_handler);
  if (nc == NULL) {
    printf("Failed to create listener\n");
    return 1;
  }

  // Set up HTTP server parameters
  mg_set_protocol_http_websocket(nc);
  s_http_server_opts.document_root = ".";  // Serve current directory
  s_http_server_opts.enable_directory_listing = "yes";

  mg_register_http_endpoint(nc, "/post_ID", handle_post_ID);

  for (;;) {
    mg_mgr_poll(&mgr, 1000);
  }
  mg_mgr_free(&mgr);

  return 0;
}

and a very simple html code

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
  <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
</head>

<body>
  <script>
    var myUrl = "/post_ID";
    console.log("myUrl: " + myUrl);
    var devID = "123";
    $(document).ready(function() {
      $.ajax({
        type: "POST",
        url: myUrl,
        data: devID,
        processData: false,
        contentType: false,
        cache: false,
        success: function(d) {
          console.log("Data: " + d);
        }
      });
    });
  </script>
</body>

</html>

The response is

Data: [I am Hello1]