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);
        }
LVL 1
chuang4630Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
What if you set URL to the full path to the server instead of a relative path - does it work?
0
chuang4630Author Commented:
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
0
Julian HansenCommented:
There are many things that could be the cause - for instance if you are using URL re-writing you may not have set this up correctly in the new environment - or your environment's are different so httpd.ini on IIS needs to be .htaccess on linux or vice versa.
You might be operating out of a sub folder so if your site is

http://some_folder/index.html

Then /Server will refer to the root folder and not some_folder

You need to look at how your two environments are configured to see what is causing the routing problem.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

chuang4630Author Commented:
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?
0
Julian HansenCommented:
a quick and dirty is to add a <base> tag to your head

<base href="http://path_to_root_folder" />

I prefer to use absolute paths
I would imagine in your case the following links are relevant
VirtualPathUtility.toAbsolute
https://msdn.microsoft.com/en-us/library/system.web.virtualpathutility.toabsolute%28v=vs.110%29.aspx
ResolveUrl
https://msdn.microsoft.com/en-us/library/system.web.ui.control.resolveurl%28v=vs.110%29.aspx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chuang4630Author Commented:
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)
0
chuang4630Author Commented:
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
0
Julian HansenCommented:
Can I take from your last post that it is now working?
0
chuang4630Author Commented:
Excellent answer!
0
Julian HansenCommented:
Thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
AJAX

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.