Home
» Javascript/Jquery
» JQUERY response data (splitting to array and then access through Foreach)
Monday, September 16, 2013
JQUERY response data (splitting to array and then access through Foreach)
Monday, September 16, 2013 by Unknown
Hiiii, JQuery itself comes with a tagline that "Write less, Do more".
So this tutorial is all about JQuery and ajax data sending and
receiving and it also explains that sometimes we receive an array in
ajax response which is bot encoded than how to split that array and use
the values in that array .
So Here's the code for this tutorial :
jQuery.each(array_data, function(index, value) {
alert(index + ‘: ‘ + value);
});
jQuery(function(){
jQuery(“#comment_form”).show();
//jQuery(“#comment_submitted”).hide();
jQuery(“#do_comment”).submit(function(){
var senddata=$(“#do_comment”).serialize();
//alert(senddata);
jQuery.ajax({
url: urlll,
data:senddata,
type:’POST’,
success: function(data){
// var response = jQuery.parseJSON(data);
var temp=0;
var array_data=data.split(” “);
//alert(array_data);
jQuery.each(array_data, function(index, value) {
if(value==”[http_code]“){
temp=index+2;
}
if(value==202 && temp==index){
//alert(index);
jQuery(“#comment_submitted”).html(“Comment Submitted”);
}
if(value==403 && temp==index){
jQuery(“#comment_submitted”).html(“Comment Already Posted”);
}
//alert(index + ‘: ‘ + value);
});
//alert(array_data.get(0));
jQuery(“#comment_form”).hide();
jQuery(“#comment_submitted”).show();
var cdata=data;
var array_data=cdata.split(” “);
}
});
return false;
})
});
So Here's the code for this tutorial :
To split a array use :
var array_data=data.split(” “); //enter the character or space in (“”) around which you wanna split your aaray .To access that data use :
jQuery.each(array_data, function(index, value) {
alert(index + ‘: ‘ + value);
});
Complete example for reference :
jQuery(function(){
jQuery(“#comment_form”).show();
//jQuery(“#comment_submitted”).hide();
jQuery(“#do_comment”).submit(function(){
var senddata=$(“#do_comment”).serialize();
//alert(senddata);
jQuery.ajax({
url: urlll,
data:senddata,
type:’POST’,
success: function(data){
// var response = jQuery.parseJSON(data);
var temp=0;
var array_data=data.split(” “);
//alert(array_data);
jQuery.each(array_data, function(index, value) {
if(value==”[http_code]“){
temp=index+2;
}
if(value==202 && temp==index){
//alert(index);
jQuery(“#comment_submitted”).html(“Comment Submitted”);
}
if(value==403 && temp==index){
jQuery(“#comment_submitted”).html(“Comment Already Posted”);
}
//alert(index + ‘: ‘ + value);
});
//alert(array_data.get(0));
jQuery(“#comment_form”).hide();
jQuery(“#comment_submitted”).show();
var cdata=data;
var array_data=cdata.split(” “);
}
});
return false;
})
});
Hope you like this post……..Please Comment…………!!!!!!!!!
Originally posted at Mycodestock
Tags: Javascript/Jquery
Subscribe to:
Post Comments (Atom)
0 Responses to “JQUERY response data (splitting to array and then access through Foreach)”
Post a Comment