In my MVC app, I use getJSON for some server calls.
However they do not work. Deeply buried in the jquery 1.4.2 library it breaks. For the e object the username and password do not exist.
Yet in the documentation for $.getJson I have seen there is no username and password that I should take care of.
So what is wrong with the code below?
var dataService = new function () {
$.ajaxSetup ({
cache: false
});
addBusinessUnit = function(employeeId, businessUnitId, callback) {
$.getJSON('<%= Url.Action("AddBusinessUnitForDepartmentAdministrator", "DataService")%>',
{ employeeId: employeeId, businessUnitId: businessUnitId },
function(data) {
callback(data);
});
},
deleteBusinessUnit = function(employeeId, businessUnitId, callback) {
$.getJSON('<%= Url.Action("DeleteBusinessUnitForDepartmentAdministrator", "DataService")%>',
{ employeeId: employeeId, businessUnitId: businessUnitId },
function(data) {
callback(data);
});
};
return {
addBusinessUnit: addBusinessUnit,
deleteBusinessUnit: deleteBusinessUnit
};
} ();
EDIT:
This is my server side code.
public ActionResult AddBusinessUnitForDepartmentAdministrator(
int employeeId, int businessUnitId)
{
var input = new DepartmentAdministratorExtraDepartment(employeeId, businessUnitId);
return new JsonResult
{
Data = input.AddNewPermission(),
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
OK I found the solution. I hope I never make this mistake again! All it was was that the JS was in it’s own file and therefore the Url.Action command could not pick up the context of the page!
I do not know if it is possible to access a page context object in JS/Jquery, but in any case all I had to do was put in a Url parameter when calling the dataservice from the page and it worked.