javascript - Web API Ajax calling not working -


here js code call custom web api method..

$.ajax({         url: '/api/adminapi/industrypost/?industryname='+addnewindustryname,         type: 'post',         cache: false,         contenttype: 'application/json; charset=utf-8',                success: function (response) {            // response code.         }     }); 

this working fine in case using 'data' tag send data, not working. following.

 $.ajax({         url: '/api/adminapi/industrypost',         type: 'post',         cache: false,         contenttype: 'application/json; charset=utf-8',         data: { 'industryname': addnewindustryname },         success: function (response) {            // response code.         }     }); 

webapiconfig config.maphttpattributeroutes();

        config.routes.maphttproute(              name: "defaultapi",              routetemplate: "api/{controller}/{action}/{id}",              defaults: new { id = routeparameter.optional }         ); 

here api method

    [actionname("industrypost")]     public void addindustry(string industryname)     {         //code here     } 

while testing fiddler second code request result 404 error. while accessing web api method directly url works fine.

answer::

after doing r'n'd on topic came know there issue in web api parameter binding. so, either can use [formuri] tag or can have model class. this

public class industryname { public string industryname { get; set; } }

and in web api method

public void industrypost(industryname industryname) { // }

no change needed in jquery.

here article explain post int part of method no http resource found matches request uri error

change below line

data: { 'industryname': addnewindustryname } 

to

data: '{ industryname:'+ addnewindustryname +'}' 

and make sure that, addnewindustryname global variable , accessible.

or can assign variable without curly braces below

data: 'industryname='+ addnewindustryname 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -