I am trying to load data from a web-api controller from within a razor page that was loaded by a standard controller.
However, when I try to execute the $.getJSON() it fails because the getJSON method adds its original controller name in front of the call. How do I go around this?
Example: a razor view gets loaded from controller MyController. I have a webapi controller called ExplorerApi where I wish to execute a method on.
So from within the razor View I call
$.getJSON("api/explorerapi/getavailablegroups?userid=marcel", function (data))
which fails because jQuery has added MyController in front and so converted it to:
$.getJSON("MyController/api/explorerapi/getavailablegroups?userid=marcel", function (data))
EDIT –
I can call the web api from the browser and it happily returns the json data by simply typing http://localhost:59475/api/ExplorerApi/getavailablegroups?userid=marcel&context=pf in the browser!
but using the code below it adds it originating controller name in front!
var uID = sessionStorage.getItem("symUserID");
var v = "userid=" + uID + "&context=portfolio";
$.getJSON("api/explorerapi/GetAvailableGroups?" + v, null, function (data) {
debugger;
})
.fail(
function (jqXHR, textStatus, err) {
debugger;
alert(err);
});
The alert message says: Not found – then if you look into the jqXHR.responseText loads of data there is the Error 404 and further down the statement it is trying to execute in which it has placed the name of the original controller!!
Requested URL http://localhost:59475/MyController/api/explorerapi/GetAvailableGroups?userid=marcel&context=portfolio&_=1358064733267
Instead of “api/explorerapi/GetAvailableGroups?” use “/api/explorerapi/GetAvailableGroups?”.