0

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.

1 Answer 1

0

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

2 Comments

But i hava an array of objects

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.