-1

I wanted to send name and password from jsp in the form of json to servlet but this is not working. What might be the problelm?

Here is my snippets.

function submitForm(thisObj, thisEvent) {
  var name = $('#name').val();
  var password = $('#password').val();

  var myData = {
    "mydata": {
      "name": name,
      "password": password
    }
  };
  $.ajax({
    type: "GET",
    url: "/Aasd",
    data: {
      jsonData: JSON.stringify(myData)
    },
    dataType: "json"
  });
  return false;
}
<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js”>
</script>


<body>
  <h2>Login</h2>
  <form enctype='application/json'>
    Name:<input type="text" value="name" id="name"><br> Password:
    <input type="password" id="password"><br>
    <input type="submit" name="submit" onclick=" return submitForm(this,event)">
  </form>
</body>
worpet
  • 3,788
  • 2
  • 31
  • 53
Bishwa Karki
  • 359
  • 4
  • 20
  • Your script tags are wrong, also your input is type="submit" and you use on click, but using submit, the click will be ignored and will try to send your form, but your form doesnt hace action, method...review some example to make it work – cralfaro Mar 07 '17 at 17:08

3 Answers3

0

The problem is you are using the GET method. You need to use POST to 'post' data to the server.

POST is made to send data, GET is made to retrieve data

GET requests should be used only to retrieve data

https://www.w3schools.com/tags/ref_httpmethods.asp

Moreover sending data via GET parameters is a bad practice as far as security is concerned (especially when using ajax to login wich is already not very secured).

$.ajax({
     type:"POST",
     url:"/Aasd",
     data: myData,
     dataType:"json"
 });

In your servlet, you will be able to retrieve the request in the doPost() method.

https://stackoverflow.com/a/2349741/5758328

Community
  • 1
  • 1
Mawcel
  • 1,967
  • 15
  • 22
0

Correct the request type from GET to POST.

$.ajax({ type:"POST"

In servlet side, from doPost() method you can call doGet() method to use the same implementation that you might hive writtent for GET type call.

Rahul Vedpathak
  • 1,346
  • 3
  • 16
  • 30
-2
$.ajax({
            type:"GET",
            url:"/Aasd",
            data:{jsonData:JSON.stringify(myData)},
            dataType:"json"
        });
        return false;

Change for

$.ajax({
            type:"POST",
            url:"/Aasd",
            data:{jsonData:JSON.stringify(myData)},
            dataType:"json"
        });
        return false;
5frags
  • 157
  • 10
  • protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/json"); PrintWriter out = response.getWriter(); try{ String action = request.getParameter("action"); String json = request.getParameter("json"); JSONObject jsonData = (JSONObject) JSONValue.parse(json); String name = (String)jsonData.get("name"); String password = (String) jsonData.get("password"); System.out.println(name + password); } } – Bishwa Karki Mar 07 '17 at 17:25
  • this is my servlet and do I need to change doGet method to doPost ? – Bishwa Karki Mar 07 '17 at 17:26
  • I changed to POST but it didn't response – Bishwa Karki Mar 07 '17 at 17:28
  • Your url is correct? – 5frags Mar 07 '17 at 17:35
  • yes It is correct. – Bishwa Karki Mar 08 '17 at 16:41