How To Create JSON Array Object With Ajax In JSP?


JSON_LOGO

If you want to know about JSON (mean how to create and what is the structure of JSON Oject ) then follow these links:
1.  http://www.jkstack.com/2013/07/how-to-create-json-object-in-javascript.html
2. http://www.jkstack.com/2013/07/how-to-change-second-dropdownlist-value.html

Today, i am gonna show you how to create JSON Array with Ajax call.
Here is the HTML code:
<input id="saveValues" type="button" value="Click me for generate JSON Array" />

Here is the Jquery code:








$("#saveValues").click(function (event){
var paramValues = {paramProperty:[]}; // 1

for(var i=0;i< 6;i++)
{
paramValues.paramProperty.push({'Id':i,'name': "Ram",'age':25}); // 2
}
var paramValuesJSON = JSON.stringify(paramValues); // 3

var data={
'userId': "User1",
'userName': "Krish",
'paramJsonValues': paramValuesJSON // 4
};
$.ajax({
type: 'POST',
url: 'saveParamValues', // 5
data: data,
dataType: 'html',
success: function(html, textStatus){
//6
},
error: function(xhr, textStatus, errorThrown) {
alert('An error occurred! ' + errorThrown+","+xhr);
}
});
});



Descriptions of Commented line numbers in the above code:
1. Declaration JSON array
2. Pushing values to JSON Array
3. A JSON-formatted string representing the values
4. Passing  JSON String  as a parameter to Ajax Call 
5. URL to be called
6.  To do something with response

Check running example here:
http://jsfiddle.net/jeetu_verma11/tzxnQ/

Share on Google Plus

About JK STACK

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

2 comments:

  1. [...] In last tutorial we discussed , how to create JSONArray of JSONObjects and pass with Ajax call in JSP.If you want to refer then visit this link: http://www.jkstack.com/2013/08/how-to-create-json-array-object-with.html [...]

    ReplyDelete
  2. it worked for keep posting like this.........

    ReplyDelete