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

406,657

Solution 1

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.

Solution 2

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]

Solution 3

you can use ajax post as :

 $.ajax({
   url: "url", 
   type: "POST",
   dataType: "json",
   contentType: "application/json; charset=utf-8",
   data: JSON.stringify({ name: 'value1', email: 'value2' }),
   success: function (result) {
       // when call is sucessfull
    },
    error: function (err) {
    // check the err for error details
    }
 }); // ajax call closing
Share:
406,657
iJared
Author by

iJared

Web Programmer @ University of Hawaii Foundation

Updated on July 16, 2022

Comments

  • iJared
    iJared almost 2 years

    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?

  • iJared
    iJared about 12 years
    I think I tried that in one of my iteration attempts. My question with this post is how to I get at the data on the server side? The hashmap statements above don't do what I need them to do.
  • Jason Snelders
    Jason Snelders about 10 years
    Even after a long time working with JSON and jQuery AJAX requests, I made the silly mistake today of writing my value for the "data" parameter as a string rather than an object. I didn't even realise I'd done it that way until I saw your answer. Goes to show you can miss problems that are staring you in the face. Thanks for jogging me straight.