Issue while submitting data to server for processing

Issue while submitting data to server for processing

pushkarpushkar Posts: 2Questions: 0Answers: 0
edited November 2011 in General
Hi,

I want to submit a certain array of data from Datatables to my Spring Controller.

I have created the array as following
[code]
var testArr = [];
testArr.push('A')
[/code]

and under the [code]$('#form').submit[/code] function

[code]
$.ajax({
url: 'run',
data: testArr,
dataType :"json",
success: function(testArr){
alert( "Data Saved: " + testArr);
}
});
[/code]

And on the Spring Controller side , my annotation looks like this

[code]
@RequestMapping(value="/run", method=RequestMethod.POST,
headers="Accept=application/json")
public String run(@RequestParam("testArr") JSON testArr) {
[/code]

When I submit the data for server side processing , it throws me an error stating that

[code]Required JSON parameter 'testArr' is not present[/code]

I am unable to understand what am I doing wrong. Please help.

Replies

  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    edited November 2011
    http://api.jquery.com/jQuery.ajax/

    in my (limited) experience with $.ajax(), the data value (i.e. your testArr, line 3 of your code above) should be either a string (querystring) or an array of objects. each object has a "name" and a "value" like this:

    { "name": "key_name", "value": "A" } // below I use "testArr[0]" as your key_name which might better create the query string you want... see below

    from
    [quote]dataObject, String
    Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. ___Object must be Key/Value pairs___. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).[/quote]

    rather than use a numeric array in your javascript, use an object/associative array
    [code]
    var testArr = {};

    // maybe "testArr[0]" isn't your best idea for a key, but I'm trying to imagine how your server side script will process the query string.. since I'm not familiar with Spring Controller, I can merely guess
    testArr["testArr[0]"] = 'A';
    [/code]

    OR

    skip the array altogether and just craft a querystring

    sArr = "testArr[0]=A"; // and then pass sArr as your "data" in $.ajax()
This discussion has been closed.