Link to home
Start Free TrialLog in
Avatar of chuang4630
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?serverId=' + serverId + '&isActive=' + isActive
            $.ajax({
                url: actionUrl,
                type: 'GET',
                dataType: "JSON",
                success: function (data) {
                    var x = data;

                },
                error: function (xhr, ajaxOptions, error) {

                    alert($.parseJSON(xhr.responseText).message);

                }
            });
        }

Controller:
        [HttpGet]
        public ActionResult SetServerStatus(int serverId, bool isActive)
        {
            log.Info("\nCall SetServerStatus.  ServerId=" + serverId.ToString());

            Server note = _serverService.GetByID(serverId);
            note.ServerStatus = isActive ? (int)ServerStatus.Active : (int)ServerStatus.Inactive;
            _serverService.Update(note);

            ServerListItem noteViewModel = Mapper.Map<ServerListItem>(note);
            return Json(new { result = "success" }, JsonRequestBehavior.AllowGet);
        }
SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chuang4630
chuang4630

ASKER

Just tried. It works. But why?

I did not use the explicit path in the other projects and they all worked well. Wondering what is missing or not configured right
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?serverId=1&isActive=true'

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)
I end up using global var.

        window.globalVar = getRoot();

        function getRoot() {
            var url = document.URL.replace("/Index","");
            var pos = url.lastIndexOf("/");
            var siteRoot = url.substring(0, pos);
            return siteRoot;
        }



var actionUrl = window.globalVar + '/Notification/SetJobStatus?notificationId=' + notificationId + '&isActive=' + isActive
Can I take from your last post that it is now working?
Excellent answer!
Thank you.