0

Hello I have been looking at other threads like mine and I can't seem to get my code working!

I need to send the JS array containing ints to a servlet this is current code:

Javascript code:

function sendReminderEmails(){
$("#sendReminderEmails").click(function(){

var people = null;
var peopleBatch1 = null;

$.ajax({
    type: "POST",
    url:"getAllUnregisteredPeople",
    async: false,  
    success: function(data){      
    people =JSON.parse(data);
  }
});

 peopleBatch1 = people.splice(0,200);

 $.post("sendReminderEmails", {people:peopleBatch1,  mode : "insert"} , function(data){

   });
 });    
}

Servlet Code:

protected void doPost(HttpServletRequest request, HttpServletResponse response){    
  String peopleIDs[] = request.getParameterValues("people");    
}

It keeps returning null! Can someone tell me if I am doing something wrong ?

techGaurdian
  • 732
  • 1
  • 14
  • 35
  • You can also try like this. [stackoverflow.com](http://stackoverflow.com/a/20561723) – Prateek Dec 01 '14 at 10:33
  • Firstly, why use `async: false`? Secondly, what is the value response from your first AJAX request? – Rory McCrossan Dec 01 '14 at 10:33
  • because I need to process the list a batch at a time, I need the first batch to finish before the second can start! Thats why I splice the array up –  Dec 01 '14 at 10:39
  • You perhaps need to parse the BODY of the posted message - check this answer: http://stackoverflow.com/questions/3831680/httpservletrequest-get-post-data – Gyro Gearless Dec 01 '14 at 10:41
  • I've tried using string buffer! Still null, when I loooked in the request parameters in the bit under queryParamters it says this "{undefined=[, , , , , , , , , , , ," –  Dec 01 '14 at 10:45

2 Answers2

0

in "$ajax" you need to pass the required parameter, eg

   var myArray={'john', 'paul'};
   $.ajax ({
            type: "POST",
            url:"getAllUnregisteredPeople",
            data: {people: myArray}
            async: false,  
            success: function(data) {      
                people = JSON.parse(data);
            }
        });
Pelit Mamani
  • 2,321
  • 2
  • 13
  • 11
  • This is a servlet to pull data from the database! in the other post I do try to pass the data along with a key –  Dec 01 '14 at 10:47
0

You must use JSON.stringify to send your JavaScript object as JSON string.

Change your code

var obj = { people: peopleBatch1, mode: "insert" };
$.post("sendReminderEmails",JSON.stringify(obj) , function(data) {});

On Servlet side you need you use

String jsonStr = request.getParameter("people");

then Convert this jsonStr to JSON object.

Honey Goyal
  • 445
  • 4
  • 22
  • I tied doing this too but on the java side I got errors because there were [] brackets in it! –  Dec 01 '14 at 10:50
  • What is the error? can you paste is here? Is this error in converting jsonStr to JSON Object? – Honey Goyal Dec 01 '14 at 10:55
  • I tried this on friday! I have chnaged my code since let me try again then ill pass it on! –  Dec 01 '14 at 10:57
  • See this [link](http://stackoverflow.com/questions/19568142/how-to-read-json-sent-by-ajax-in-servlet) – Honey Goyal Dec 01 '14 at 12:06