Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I'm new to JavaScript and jQuery. I'm dynamically creating objects of rows where the checkbox is clicked in my jsp like this:

 var objects={};
 var i=0;
 $(document).on('click', 'input[type="checkbox"]', function() {
       var bar1 = $(this).closest("tr").find(".bar").val();
       var dda1 = $(this).closest("tr").find(".dda").text();
       objects[i] = {data:dda1,bar:bar1};
       i++;
       $('#bar').val(JSON.stringify(objects));
 });

and firstly was setting it to a hidden input type like this...

  $('#bar').val(JSON.stringify(objects));

I was able to read it using the hidden type using the below controller...

@RequestMapping(value="/applypage",method= RequestMethod.POST)   

public String ListRequest(@RequestParam("bar") String object )
{
    System.out.println(object);
    return "applypage";
}

which gives me the following String...

{"0":{"data":"Data3","bar":"N"},"1":{"data":"Data1","bar":"Y"},"2":{"data":"Data4","bar":"N"},"3":{"data":"Data6","bar":"N"}}

But after a search on google and referring to many tutorials I found that I have to use ajax and then I tried this code...

var data={'objects':JSON.stringify(objects)};
$.ajax({
            type: "post",
            url: "applypage", //your valid url
            headers : {
                'Accept' : 'application/json',
                'Content-Type' : 'application/json'
            },
            data: data,
            success: function(result) {
                alert("success");
            },
            error: function(e){
                alert('failure');
            }
 });

But I'm unable to read this value through ajax...

Can anyone please help me in this and I need the code to write the controller also to read the JSON values.

share|improve this question

1 Answer 1

Add dataType: "json", in your ajax code

$.ajax({
            type: "post",
            url: "applypage", //your valid url
            headers : {
                'Accept' : 'application/json',
                'Content-Type' : 'application/json'
            },
            data: data,
            dataType: "json"
            success: function(result) {
                alert("success");
            },
            error: function(e){
                alert('failure');
            }
 });
share|improve this answer
    
And what about controller how do i read it controller – Monica Jain Sep 26 '14 at 7:12
    
refer this stackoverflow.com/questions/20245544/… – ssm Sep 26 '14 at 7:25
    
But i hava an array of objects – Monica Jain Sep 26 '14 at 7:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.