how to send whole array to servlet and how to access it - javascript

I am trying to get all jqgrid data in one array and sending to servelet,so far I am try with this-
var rows= jQuery("#list").jqGrid('getRowData');
var paras=new Array();
for(var i=0;i<rows.length;i++)
{
var row=rows[i];
paras.push($.param(row));
//alert(paras[i]);
}
alert(paras);
$.ajax({
type: "POST",
url: "JQGridServlet?action=arraydata&paras="+paras,
data: paras.join('and'),
success: function(msg)
{
alert(msg);
}
});
but it send only first 'srno'.not whole array.
please any body suggest me to how to send array to servlet and how to access it on servlet.

Try this:
var griddata= $( "#list" ).getRowData();
var model = {
grid: griddata
};
var paras= JSON.stringify( model );
alert(paras);
$.ajax({
type: "POST",
url: "JQGridServlet?action=arraydata&paras="+paras,
data: paras.join('and'),
success: function(msg)
{
alert(msg);
}
});

Related

I don't know if I use button id jquery ajax serialize right, but doesn't work

$('button.red-button').on('click', function(form)
{
form.preventDefault();
var obj = {};
obj.id = $(this).attr('id');
//var id['id'] = $(this).attr('id');
alert(JSON.stringify(obj));
$.ajax(
{
type: 'POST',
dataType: 'json',
url: 'delete-comment',
data: JSON.stringify(obj),
success: function(data)
{
alert(data);
},
error: function(data)
{
alert(0);
}
});
});
This is what I made.
For example we have this button
<button id="4" class="red-button">Usuń</button>
This script will alert first
{"id":"4"}
So it alerts it's ID in JSON format.
This is what i wanted, to delete this item by doing AJAX POST request.
But in PHP by doing
echo json_encode($this->input->post('id'));
I got NULL in second alert.
or
echo json_encode($_POST['id']);
and i got 0 in second alert
I'm doing something wrong but I don't know...
Maybe this json is send wrongly, or json is build wrongly idk
$('button.red-button').on('click', function(form)
{
form.preventDefault();
var obj = {};
obj.id = $(this).attr('id');
//var id['id'] = $(this).attr('id');
alert(JSON.stringify(obj));
$.ajax(
{
type: 'POST',
dataType: 'json',
url: 'delete-comment',
data: obj,
success: function(data)
{
alert(data);
},
error: function(data)
{
alert(0);
}
});
});
Use it like that and it will work, without stringify the object

Send form elements and array through $.ajax()

I need to pass through $.ajax() method some form elements and an array too. How can i send serialize and array by ajax?
my code bellow:
function loadgraficosajax(){
var arr = ['331234','142323','327767'];
var data = $('#p-form').serialize;
$.ajax({
type: "POST",
url: "/page/show",
data: data,
dataType : 'html',
success: function (msg) {
$(document).ajaxComplete(function (event, request, settings) {
$('.has-error').removeClass('has-error');
$(document).off('ajaxComplete').off('ajaxSend');
$('#addajax').html(msg);
});
}
});
}
serialize is a method in jQuery, not a property, so you should call it in this way:
var data = $('#p-form').serialize();
and to pass your array you need to use param method and modify your array to be inside Object, where array name is object property:
var arr = { arr: ['331234','142323','327767'] };
var data = $('#p-form').serialize();
data += '&' + $.param( arr );
param will transform your object to serialised string:
console.log($.param( arr )); // "arr[]=331234&arr[]=142323&arr[]=327767"
var formData = new FormData($('#p-form')[0]);
var arr = ['331234','142323','327767'];
// append array to formData
formData.append('arr',JSON.stringify(arr));
$.ajax({
type: "POST",
url: "/page/show",
data: formData,
success:.....
You could bundle the two items together in a stringified object which you can send:
function loadgraficosajax(){
var arr = ['331234','142323','327767'];
var data = $('#p-form').serialize();
var request = {
array: arr,
elements: data
}
var data = JSON.stringify(request);
$.ajax({
type: "POST",
url: "/page/show",
data: data,
dataType : 'json',
success: function (msg) {
$(document).ajaxComplete(function (event, request, settings) {
$('.has-error').removeClass('has-error');
$(document).off('ajaxComplete').off('ajaxSend');
$('#addajax').html(msg);
});
}
});
}

How to fix this undefined index error? Ajax to PHP

Notice: Undefined index: query in C:\xampp\htdocs\Java\Search\instant-search.php on line 71
{"names":[]}
This is my Php
$query=$_POST["query"];
$matchType=isset($_POST["match_type"])? $_POST["match_type"]:MatchType::CONTAINS;
processRequest($query,$matchType);
Here is my ajax script
$("#query").keyup(function(){
var q=$(this).val();
var match_type=$("input[type=radio]:checked").val();
var data={'query':q,'match_type':match_type};
if(q.length==0){
$("#results").html("");
return false;
$.ajax({
url:"/Java/Search/instant-search.php",
data:data,
type:"post",
dataType:"json",
success:function(res) {
var tmpl=$("#names_tmpl").html();
var html=Mustache.to_html(tmpl,res);
$("#results").html(html);
}
});
As per your comments if both of them match_type and q are getting values than use ajax with data as:
$.ajax({
url:"/Java/Search/instant-search.php",
data: "query="+q+"&match_type="+match_type ,
type:"post",
dataType:"json",
success:function(res) {
var tmpl=$("#names_tmpl").html();
var html=Mustache.to_html(tmpl,res);
$("#results").html(html);
}
});
To change the type of request use method property not type and use Uppercase string POST jQuery.ajax:
$.ajax({
url: "/Java/Search/instant-search.php",
data: data,
method: "POST",
dataType: "json",
success: function(res) {
var tmpl=$("#names_tmpl").html();
var html=Mustache.to_html(tmpl,res);
$("#results").html(html);
}
});

How to add each jquery array values inside the single quotes

I have a array in jquery.Now as per my need i have to add each array values into single quotes as ..
var toc='INCOMING','INETCALL','ISD','LOCAL','STD'
But at present i have values like this ..
var toc=INCOMING,INETCALL,ISD,LOCAL,STD
And here is my codes ..
$.ajax({
type: 'GET',
url: 'getdata',
async:false,
dataType: "text",
success: function(data) {
var values = [];
values = data;
values=values.replace('[','');
values=values.replace(']','');
var array = values.split(",");
for(var i=0,len=array.length;i<len;i++)
{
if($.isNumeric(array[i]))
{
callcost.push(array[i]);
}
else
{
toc.push(array[i]);
}
}
alert(toc);
alert(callcost);
}
});
not sure if i got your question right but i guess you are messing up with all this replace/split/... logic. If the data object is an array just try this
$.ajax({
type: 'GET',
url: 'getdata',
async:false,
dataType: "text",
success: function(data) {
var array = JSON.parse(data);
$.each(array, function(i, val){
if($.isNumeric(val)) {
callcost.push(val);
}else{
toc.push(val);
}
});
}
});

Can I send the value of html like this $("#id").html() to code behind?

I have already tried it via ajax but it doesn't work so Help me please !!!!!!
And i tried it into cookie but in code behind didnt see it
var datah = $(“#currid”).html()
var data = {
id: currid,
html: datah
};
$.ajax({
type: "POST",
url: url,
data: data,
success: function(msg){
alert( "return back" );
}
});
Try this
You must encode the html content before transmitting . You can use escape in java script for this.And you can decode back it from server
var encodedData= escape($(“#id”).html()) ;
var postData = { htmlData:encodedData};
$.ajax({
type: "POST",
url: url,
data: postData,
success: function(msg){
// do your operation
}
});

Categories