asp.net mvc - Making AJAX call to MVC API -


i trying make api call ajax:

svc.authenticateadmin = function (id, code) {     $.ajax({         url: 'api/event/authenticate',         data: { 'id': id, 'code': code },         datatype: 'json',         contenttype: 'application/json',         type: 'get',         success: function (data) {             app.eventbus.publish('authenticationcomplete', data);         }     }); }; 

the method in api controller:

[actionname("all")] public bool authenticate(int id, string code) {     var repo = new mongorepository<event>(_connectionstring);     var entry = repo.firstordefault(e => e.id == id);     return entry.adminpassword == code; } 

but getting 404 error: urlstuff/api/event/authenticate?id=123&code=abc 404 (not found)

i have copied implementation number of known working calls (that did not write). like:

svc.geteventfromcode = function (code) {     $.ajax({         url: '/api/event/',         data: { 'code': code },         datatype: 'json',         type: 'get',         success: function (data) {             app.eventbus.publish('loadedevent', data);             app.eventbus.publish('erroreventcodeexists');         },         error: function () {             app.eventbus.publish('eventnotfound', code);         }     }); }; 

and

svc.geteventpage = function (pagenumber) {     $.ajax({         url: '/api/event/page/',         data: { 'pagenumber': pagenumber },         datatype: "json",         contenttype: "application/json",         type: 'get',         success: function (data) {             app.eventbus.publish('loadednexteventspage', data);         }     }); }; 

but neither has pass in 2 parameters api. i'm guessing it's minor :/

the problem lies in url.

apparently, ajax interpret / @ start of url root

when application deployed on serverserver, url http://localhost:8080/appname/

with api/event/page/, ajax resolve url http://localhost:8080/appname/api/event/page/ or url relative current directory.

however, /api/event/page/, url resolved http://localhost:8080/api/event/page/

hope helped.


Comments