bdfios
bdfios

Reputation: 667

Sending jquery array and receiving it in jsp

I stored some integer value in an array called myArray. I want to use Ajax to send myArray to a jsp file (file.jsp). I'm having trouble retrieving the data in jsp, as I always get null. Here is my code:

var request = $.ajax({
   url: "file.jsp",
   type: "POST",
   data: {myArray:myArray},
   dataType: "html"
});

request.done(function(msg) {
  $("#abc").html( msg );
});
request.fail(function(jqXHR, textStatus) {
  alert( "Failed " + textStatus );
});

file.jsp

String myArray = request.getParameter("spArray");

My question is: How can I successfully pass myArray from jquery-Ajax and retrieve it in file.jsp?

Upvotes: 0

Views: 2620

Answers (2)

bdfios
bdfios

Reputation: 667

I think I figured it out. I converted array to string (ie myArray.toString();) and sent through.

Now I have:

var request = $.ajax({
  url: "file.jsp",
  type: "POST",
  data: {myArray:myArray.toString()},
  dataType: "html"
});

and picked it up in file.jsp as

String myArray = request.getParameter("spArray").toString();

Upvotes: 1

Atep
Atep

Reputation: 486

try change this parametrs in ajax configuration:

dataType:       'json',
headers:        {'Content-type' : "application/json; charset=utf-8"},

Upvotes: 0

Related Questions