javascript - AJAX call a function with a parameter -


the following gives me internal server error.

var jsonstatedata;     $.ajax({         type: "post",         url: "functions.aspx/statesalesdatastring",         data: '{' + 'al' + '}',         datatype: "json",         contenttype: "application/json; charset=utf-8",         success: function (data) {             jsonstatedata = $.parsejson(data.d);         }     }).done(function () {         console.log(jsonstatedata);      }) 

this function calling

//returns stores sales datatable [webmethod] public static string statesalesdatastring(string whichstate) {     sqlconnection conn = new sqlconnection(configurationmanager.connectionstrings["dashboard_vwconnectionstringtest"].connectionstring);     conn.open();      string storesalesquery = "select storesalestbl.storenumb, lat, lng, todaytotalsales, todaytotalorders, todaytotalwebsales, todaytotalcallsales, todaytotalifsales, todaytotalstoresales, todaytotalweborders, todaytotalcallorders, todaytotaliforders, todaytotalstoreorders " +                              "from storesalestbl inner join storeaddretbl " +                              "on storesalestbl.storeid = storeaddretbl.storeid " +                              "where datetimeto in (select max(datetimeto) storesalestbl) " +                              "and storeaddretbl.state = @statename";      sqlcommand storecomm = new sqlcommand(storesalesquery, conn);     storecomm.parameters.addwithvalue("@statename", whichstate);      storecomm.commandtype = commandtype.text;     // create dataadapter run command , fill datatable     sqldataadapter dataadpt = new sqldataadapter();     dataadpt.selectcommand = storecomm;     datatable storedatatbl = new datatable();     dataadpt.fill(storedatatbl);      conn.close();      //convert datatable string     system.web.script.serialization.javascriptserializer serializer = new system.web.script.serialization.javascriptserializer();     list<dictionary<string, object>> rows = new list<dictionary<string, object>>();     dictionary<string, object> row;     foreach (datarow dr in storedatatbl.rows)     {         row = new dictionary<string, object>();         foreach (datacolumn col in storedatatbl.columns)         {             row.add(col.columnname, dr[col]);         }         rows.add(row);     }     return serializer.serialize(rows); } 

update thank guys answering! think problem might in c# function. doing wrong query statement @statename part?

because when changed query's last line just

and storeaddretbl.state = 'al'" 

it worked. when pass string whichstate in there, didn't proceed.

your json invalid. have looking this:

{'al'} 

it should looking similar this:

{ "whichstate": "al" } 

the proper way in javascript:

var mydata = {}; mydata.whichstate = 'al';  var jsonstatedata;     $.ajax({         type: "post",         url: "functions.aspx/statesalesdatastring",         data: json.stringify(mydata),         datatype: "json",         contenttype: "application/json; charset=utf-8",         success: function (data) {             jsonstatedata = $.parsejson(data.d);         }     }).done(function () {         console.log(jsonstatedata);      }) 

notice on data parameter, i'm using json.stringify. javascript json.stringify function automatically create json string you.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -