0

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?

2 Answers 2

1

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();
Sign up to request clarification or add additional context in comments.

Comments

0

try change this parametrs in ajax configuration:

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

4 Comments

in jsp, value is still null
hmm, and now? data: JSON.stringify({myArray:myArray}),
It doesn't seem to execute the ajax portion of the code. Here is what I have now var request = $.ajax({ url: "file.jsp", type: "POST", dataType: 'json', data: JSON.stringify({myArray:myArray}), ContentType : "application/json; charset=utf-8" });
I tried this: var myArray = [1,2,3]; $.ajax({ url: "file.jsp", type: "POST", dataType: 'json', data: JSON.stringify({myArray:myArray}), ContentType : "application/json; charset=utf-8" }); And the ajax was send and output is: '{"myArray":[1,2,3]}' try look at firebug

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.