c# - parameters not being passed in to api controller from $http.post -


angularjs:

$http.defaults.headers.post["content-type"]= "application/x-www-form-urlencoded";  $http({     url: 'http://localhost:17438/api/people/postperson/',     method: "post",     data: { name: vm.parent[i].name, dob: '01/15/2001', email: vm.parent[i].email, phone: vm.parent[i].cell, carriername: vm.parent[i].carrier, persontypeid: 1 } }) .then(function (response) {     // success     alert('sucess : ' + response); }, function (response) { // optional     // failed     alert('failure : ' + response); }); 

i've tried variation:

var data = { name: vm.parent[i].name, dob: '01/15/2001', email: vm.parent[i].email, phone: vm.parent[i].cell, carriername: vm.parent[i].carrier, persontypeid: 1 }; $http.post('http://localhost:17438/api/people/postperson/', data); 

parameters being passed:

{"name":"jv","dob":"01/15/2001","email":"j@live.com","phone":"5551212","carriername":"sprint","persontypeid":1}: 

webapi:

[httppost] [httpoptions] public string postperson(newuserregistration newreg) {     var json = jsonconvert.serializeobject(0);     person myperson = new person();     myperson.personname = newreg.name;     myperson.personemail = newreg.email;     myperson.personphone = newreg.phone;     myperson.personphonecarrier = newreg.carriername;     myperson.persondob = newreg.dob;     myperson.familyid = newreg.familyid;     myperson.persontypeid = newreg.persontypeid;      db.people.add(myperson);     db.savechanges();      return "got here"; }  public class newuserregistration {     public string name { get; set; }     public string email { get; set; }     public string phone { get; set; }     public string carriername { get; set; }     public datetime dob { get; set; }     public string registrationid { get; set; }     public int familyid { get; set; }      public int persontypeid { get; set; } } 

parameter population:

i know hard read can see values i'm passing in not being passed newuserregistration object

enter image description here

i've looked @ several questions on stack seems reference type of issue.

angular post web api doesn't pass data

issue in angularjs $http.post webapi

this project using 1.3.15 - i'm not sure if upgrading 1.5 help?

what missing on this?

update:

there comment gone, stringified data suggested:

var data = json.stringify({ name: vm.parent[i].name, dob: '01/15/2001', email: vm.parent[i].email, phone: vm.parent[i].cell, carriername: vm.parent[i].carrier, persontypeid: 1 });  $http.post('http://localhost:17438/api/people/postperson/', data); 

i noticed strange though. calling api method 2 times, 1st time has null data (as witnessed originally), calls 2nd time , data there!

i'm not sure why being called twice though? there i'm doing incorrectly i'm stringifying data?

update double call:

i noticed following: enter image description here

you notice 1 says options , other says post. webapi has following tags:

[httppost] [httpoptions] 

if removed options, fails (can't find 404). these tags have this?

use json.stringify() wrap json

var url = 'http://localhost:17438/api/people/postperson/'; var data = json.stringify({ name: vm.parent[i].name, dob: '01/15/2001', email: vm.parent[i].email, phone: vm.parent[i].cell, carriername: vm.parent[i].carrier, persontypeid: 1 }); $http.post(url, data); 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -