Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Not getting response while using doPost Path Based Servlet

Avatar

Level 2

Hi, I am trying to use and explore the doPost Servlet, here I am just writing a text as response, also written an AJAX call in the HTML file of a TEST component and then I am inserting that component to the page. Please refer the below snaps.

vikas101_0-1661680847509.png

vikas101_0-1661681439596.png

But this is not working, getting an error when I am hitting the Path of the servlet (refer the below snap), but when I changed this servlet to "doGet" then it is working fine. So, what specifically is wrong here in doPost method?

vikas101_1-1661680975683.png

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

You can't hit post request from browser url path.

use postman to test post request.

View solution in original post

3 Replies

Avatar

Correct answer by
Community Advisor

You can't hit post request from browser url path.

use postman to test post request.

Avatar

Level 2

@arunpatidar In addition to the above I have a scenario: I have two input fields and a submit button in the HTML and now have to take the input(s) from there on clicking the Submit button and have to POST it on the servlet with will return it as a JSON response. So, for that I have written an HTML, AJAX call so what would be the next step. Please help!  
HTML code below for reference:

  <label for="fname">First name:</label>
  <input type="text" id="fname" name="firstname" ><br><br>
  <label for="lname">Last name:</label>
  <input type="text" id="lname" name="lastname" ><br><br>
  <button type="button" id="submit-btn">Submit</button>

AJAX call script for reference:

<script>
$(document).ready(function() {
    var $fname = $("#fname");
    var $lname = $("#lname");
    $("#submit-btn").on('click', function(){
        var nameInfo = {
        firstname: $fname.val(),
        lastname: $lname.val(),
    };
    console.log(nameInfo); 

    $.ajax({
        type : "POST",
        url : '/bin/storedata/writeval',
        data : nameInfo,
        success : function(item) {
          console.log("FNAME: " + item.firstname + ", LNAME: " + item.lastname);
        },
        error : function(item) {
           console.log("FNAME: " + item.firstname + ", LNAME: " + item.lastname);
           console.log("Entered the Error function");
        }
    });
    });
});
</script>

Also, the AJAX call is going in error function plus "item.firstname" and "item.lastname" is returning "undefined".