Problem with Post request in my ASP.NET Web API + jQuery
-
Hi, I have an issue, that when I send get request to my ASP.NET Web API, also my post function in my controller gets called. Here is my code, I'd appreciate if anyone could help. I'd like to know what do I do wrong when I declare my post function that it also gets called with my get request. both my get and post function in my controller: public IHttpActionResult GetAffiliate(int id) { Classes.Data d = new Classes.Data(); Tuple result = d.getAffiliateById(id); Int16 status = result.Item1; Affiliate affiliate = result.Item2; if (status == 0) { return NotFound(); } return Ok(affiliate); } [AcceptVerbs("POST")] public HttpResponseMessage Post([FromBody]Address address) { Classes.Data d = new Classes.Data(); d.addressEdit(address); return Request.CreateResponse(HttpStatusCode.OK, "Address changed."); } my jQuery code where I apply get request: function getInfo() { //point to the api var uri = 'api/affiliate'; //clear the list div first $("#aff_name").empty(); var aff_id = $('#aff_id').val(); $.getJSON(uri + '/' + aff_id) .done(function (data) {... and here is my post request in jquery: $.ajax({ type: "POST", data: JSON.stringify(address), url: "api/affiliate/", contentType: "application/json" }); Thank you so much for your consideration.
-
Hi, I have an issue, that when I send get request to my ASP.NET Web API, also my post function in my controller gets called. Here is my code, I'd appreciate if anyone could help. I'd like to know what do I do wrong when I declare my post function that it also gets called with my get request. both my get and post function in my controller: public IHttpActionResult GetAffiliate(int id) { Classes.Data d = new Classes.Data(); Tuple result = d.getAffiliateById(id); Int16 status = result.Item1; Affiliate affiliate = result.Item2; if (status == 0) { return NotFound(); } return Ok(affiliate); } [AcceptVerbs("POST")] public HttpResponseMessage Post([FromBody]Address address) { Classes.Data d = new Classes.Data(); d.addressEdit(address); return Request.CreateResponse(HttpStatusCode.OK, "Address changed."); } my jQuery code where I apply get request: function getInfo() { //point to the api var uri = 'api/affiliate'; //clear the list div first $("#aff_name").empty(); var aff_id = $('#aff_id').val(); $.getJSON(uri + '/' + aff_id) .done(function (data) {... and here is my post request in jquery: $.ajax({ type: "POST", data: JSON.stringify(address), url: "api/affiliate/", contentType: "application/json" }); Thank you so much for your consideration.
Are you sure you're simply not running both bits of javascript?