Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send array as postData parameter

I just started working with jqGrid this week. I'm attempting to make a generic function that I can call to load a jqGrid with different postData parameters, defined in the function call.

Is there any way to do this? What I had considered was creating an array to store the parameter's name/value, then setting that as a parameter in the postData. The web service would then go through and add those parameters to the SQL stored procedure call.

Here is the generic code:

var paramArray = new Array();
var p1 = new JQGridParam("CustomerName","John");
var p2 = new JQGridParam("ProductName","Kleenex");
var p3 = new JQGridParam("YearPurchased","2012");

paramArray[0] = p1;
paramArray[1] = p2;
paramArray[2] = p3;

$("#list").jqGrid({
     datatype: "json",
     url: "services/Customers.asmx/GetCustomerData",
     mtype: "POST",
     postData: {
         sqlParams: paramArray         //HERE IS WHERE I NEED THE HELP
     },
     ajaxGridOptions: { contentType: "application/json; charset=utf-8" },
     ajaxRowOptions: { contentType: "application/json; charset=utf-8" },
     serializeGridData: function (postData) {
                  return JSON.stringify(postData);
     },
     serializeRowData: function (postData) {
                  return JSON.stringify(postData);
     },
     jsonReader: {
         root: function (obj) { return obj.d.rows; },
         page: function (obj) { return obj.d.page; },
         total: function (obj) { return obj.d.total; },
         records: function (obj) { return obj.d.records; },
     },
     colModel: [
         {name: "CustID", index: "CustID" },
         {name: "PurchaseAmount", index: "PurchaseAmount" }],
     colNames: ["Customer ID", "Amount Purchased"],
     pager: $("#pager"),
     loadOnce: true,
     rowNum: 10,
     rowList: [10,25,50],
     gridview: true,
     viewrecords: true
});

When I try this, it throws the error "cannot convert object of type System.String to type System.Array". Anyone have any ideas how to go about this?

like image 671
user1147941 Avatar asked Sep 01 '26 13:09

user1147941


1 Answers

JSON stringify the array.

JSON.stringify(paramArray);

Then in your webservice cast the JSON data back to your object.

like image 127
DisplayName Avatar answered Sep 03 '26 02:09

DisplayName



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!