c# - How to pass JSON string via Angular $http using a variable instead of function(param)? -
i have json object so:
var newcontact = {"contact_name":"joe","contact_cell_phone":"4651354"} and need pass through c# asp backend using angular $http.post such as:
$scope.add = function (newcontact) { // passing in string variable "newcontact_string", not function param "newcontact" var newcontract_string = json.stringify(newcontact); $http .post('default.aspx/adddb_contact', { newcontact_string: newcontact_string }) .success(function (data) { console.log("new contact added id: " + data.d); }) .error(function () { console.log("an error occurred adding new contact."); }); }; however, hit error in js console, referring line of code:
referenceerror: newcontact_string not defined >> $http.post('default.aspx/adddb_contact', { newcontact_string: newcontact_string }) my c# backend code never reached:
[webmethod] public static object adddb_contact(string newcontact_string) { return newcontact_string; } perhaps syntax error? how go ajaxing angular way?
you have typo.
you declare var newcontract_string = json.stringify(newcontact);
but pass newcontact_string.
you need: { newcontact_string: newcontract_string }
Comments
Post a Comment