Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery $.ajax() post - data in a java servlet

I want to send data to a java servlet for processing. The data will have a variable length and be in key/value pairs:

{ A1984 : 1, A9873 : 5, A1674 : 2, A8724 : 1, A3574 : 3, A1165 : 5 } 

The data doesn't need to be formated this way, it is just how I have it now.

var saveData = $.ajax({       type: "POST",       url: "someaction.do?action=saveData",       data: myDataVar.toString(),       dataType: "text",       success: function(resultData){           alert("Save Complete");       } }); saveData.error(function() { alert("Something went wrong"); }); 

The $.ajax() function works fine as I do get an alert for "Save Complete". My dilemna is on the servlet. How do I retrieve the data? I tried to use a HashMap like this...

HashMap hm = new HashMap(); hm.putAll(request.getParameterMap()); 

...but hm turns out to be null which I am guessing means the .getParameterMap() isn't finding the key/value pairs. Where am I going wrong or what am I missing?

like image 648
iJared Avatar asked Apr 18 '12 17:04

iJared


People also ask

Can AJAX use post?

post() makes Ajax requests using the HTTP POST method. The basic syntax of these methods can be given with: $. get(URL, data, success); —Or— $.

What is AJAX in Java servlet?

Ajax (also AJAX), an acronym for Asynchronous JavaScript and XML, is a group of interrelated web development techniques used on the client-side to create asynchronous web applications. With Ajax, web applications can send data to, and retrieve data from, a server asynchronously.

Is AJAX get or post?

GET vs POST in AJAX calls Unless you are sending sensitive data to the server or calling scripts which are processing data on the server it is more common to use GET for AJAX calls. This is because when using XMLHttpRequest browsers implement POST as a two-step process (sending the headers first and then the data).


2 Answers

You don't want a string, you really want a JS map of key value pairs. E.g., change:

 data: myDataVar.toString(), 

with:

var myKeyVals = { A1984 : 1, A9873 : 5, A1674 : 2, A8724 : 1, A3574 : 3, A1165 : 5 }    var saveData = $.ajax({       type: 'POST',       url: "someaction.do?action=saveData",       data: myKeyVals,       dataType: "text",       success: function(resultData) { alert("Save Complete") } }); saveData.error(function() { alert("Something went wrong"); }); 

jQuery understands key value pairs like that, it does NOT understand a big string. It passes it simply as a string.

UPDATE: Code fixed.

like image 100
aquinas Avatar answered Sep 24 '22 16:09

aquinas


Simple method to sending data using java script and ajex call.

First right your form like this

<form id="frm_details" method="post" name="frm_details"> <input  id="email" name="email" placeholder="Your Email id" type="text" />     <button class="subscribe-box__btn" type="submit">Need Assistance</button> </form>  

javascript logic target on form id #frm_details after sumbit

$(function(){         $("#frm_details").on("submit", function(event) {             event.preventDefault();              var formData = {                 'email': $('input[name=email]').val() //for get email              };             console.log(formData);              $.ajax({                 url: "/tsmisc/api/subscribe-newsletter",                 type: "post",                 data: formData,                 success: function(d) {                     alert(d);                 }             });         });     })       General  Request URL:https://test.abc Request Method:POST Status Code:200  Remote Address:13.76.33.57:443  From Data email:[email protected] 
like image 20
Harish Verma Avatar answered Sep 22 '22 16:09

Harish Verma