chuang4630
asked on
JQuery ajax call returns 404
This code works fine when it is running locally (IISExpress). But it fails when I deploy it to the dev server.
It is confirmed that the controller is not hit when I make this call.
I have spend a lot of time to work on but still cannot figure it out. Really appreciate for any help. Big Thanks in advance
[GET] http://mydomain.net/Server/SetServerStatus?serverId=2&isActive=false
404 (Not Found)
code:
function SetServerStatus(serverId, isActive) {
var actionUrl = '/Server/SetServerStatus?s erverId=' + serverId + '&isActive=' + isActive
$.ajax({
url: actionUrl,
type: 'GET',
dataType: "JSON",
success: function (data) {
var x = data;
},
error: function (xhr, ajaxOptions, error) {
alert($.parseJSON(xhr.resp onseText). message);
}
});
}
Controller:
[HttpGet]
public ActionResult SetServerStatus(int serverId, bool isActive)
{
log.Info("\nCall SetServerStatus. ServerId=" + serverId.ToString());
Server note = _serverService.GetByID(ser verId);
note.ServerStatus = isActive ? (int)ServerStatus.Active : (int)ServerStatus.Inactive ;
_serverService.Update(note );
ServerListItem noteViewModel = Mapper.Map<ServerListItem> (note);
return Json(new { result = "success" }, JsonRequestBehavior.AllowG et);
}
It is confirmed that the controller is not hit when I make this call.
I have spend a lot of time to work on but still cannot figure it out. Really appreciate for any help. Big Thanks in advance
[GET] http://mydomain.net/Server/SetServerStatus?serverId=2&isActive=false
404 (Not Found)
code:
function SetServerStatus(serverId, isActive) {
var actionUrl = '/Server/SetServerStatus?s
$.ajax({
url: actionUrl,
type: 'GET',
dataType: "JSON",
success: function (data) {
var x = data;
},
error: function (xhr, ajaxOptions, error) {
alert($.parseJSON(xhr.resp
}
});
}
Controller:
[HttpGet]
public ActionResult SetServerStatus(int serverId, bool isActive)
{
log.Info("\nCall SetServerStatus. ServerId=" + serverId.ToString());
Server note = _serverService.GetByID(ser
note.ServerStatus = isActive ? (int)ServerStatus.Active : (int)ServerStatus.Inactive
_serverService.Update(note
ServerListItem noteViewModel = Mapper.Map<ServerListItem>
return Json(new { result = "success" }, JsonRequestBehavior.AllowG
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
You are right. Folder is the reason.
Some of the JS code are part of chtml. So how do I specify a global variable from the configration, or dynamically detect it?
Some of the JS code are part of chtml. So how do I specify a global variable from the configration, or dynamically detect it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It works if you make ajax call to the same controller. But if you make ajax call to the different controller, it does not work.
<base href="http://brcesdev.risk.regn.net/OrbitHealthMonitor/" target="_blank"/>
var actionUrl2 = '/Server/SetServerStatus?s erverId=1& isActive=t rue'
The "/" before the Server ignores the folder "OrbitHealthMonitor" and take it directly to the root.
If you Use "Server/Set.....", then relative path respects the folder "OrbitHealthMonitor", but it limit to the same controller. If you call the different controller, then you have to use the absolute path. That will cause the problem (for code maintenance)
<base href="http://brcesdev.risk.regn.net/OrbitHealthMonitor/" target="_blank"/>
var actionUrl2 = '/Server/SetServerStatus?s
The "/" before the Server ignores the folder "OrbitHealthMonitor" and take it directly to the root.
If you Use "Server/Set.....", then relative path respects the folder "OrbitHealthMonitor", but it limit to the same controller. If you call the different controller, then you have to use the absolute path. That will cause the problem (for code maintenance)
ASKER
I end up using global var.
window.globalVar = getRoot();
function getRoot() {
var url = document.URL.replace("/Ind ex","");
var pos = url.lastIndexOf("/");
var siteRoot = url.substring(0, pos);
return siteRoot;
}
var actionUrl = window.globalVar + '/Notification/SetJobStatu s?notifica tionId=' + notificationId + '&isActive=' + isActive
window.globalVar = getRoot();
function getRoot() {
var url = document.URL.replace("/Ind
var pos = url.lastIndexOf("/");
var siteRoot = url.substring(0, pos);
return siteRoot;
}
var actionUrl = window.globalVar + '/Notification/SetJobStatu
Can I take from your last post that it is now working?
ASKER
Excellent answer!
Thank you.
ASKER
I did not use the explicit path in the other projects and they all worked well. Wondering what is missing or not configured right