Link to home
Start Free TrialLog in
Avatar of flynny
flynnyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

MVC Routing for AJAX call not working when on server?

HI all,

I have a successful AJAX Call to a controller method /Home/SendForm which is working perfectly locally on my machine.

However when I load it to our server I am getting a 404 error returning the the $.ajax() error method?

Now all files have successfully been uploaded (including routing) to the server. Has anyone seen this error before?

The server is running IIS 7.5.
Avatar of flynny
flynny
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Just to add my ajax call is as follows;

 $.ajax({
                    url: '@Url.Action("SendForm", "Home")', 
                    contentType: "application/json",
                    type: "POST",
                    datatype: "json",
                    data: JSON.stringify(form),
                    error: function (xmlHttpRequest, errorText, thrownError) {
                        alert(xmlHttpRequest + "|" + errorText + "|" + thrownError);
                        alert(xmlHttpRequest.responseText);
                        alert(xmlHttpRequest.statusText);
                    },
                    success: function (data) {
                        //alert(data);
                        if (data) {
                            $('#submitting').slideUp("slow");
                            $('#complete').slideDown("slow");
                        }
                    }
                });

Open in new window

Avatar of kaufmed
Have you run Fiddler (or equivalent) along side to see what is actually being sent to the server?
When you make an AJAX request to "/Some/Path" from a file, lets say, "http://your.domain.com/folder2/file1", the url requested will be fully formed as "http://your.domain.com/Some/Path" or in your case "http://your.domain.com/Home/SendForm". Are you sure your routes are set up to handle the request at this URL?
Avatar of flynny

ASKER

and my post method in my home controller is

 [HttpPost]
        public Boolean SendForm(InstantQuote form) {

Open in new window

Avatar of flynny

ASKER

@Kaufmed - no I havent used fiddler I will download and test now.

@james - I have checked the routing and should be all ok? The is no cross domain with the call and I have tried hardocding the url, i.e. http://mydomain.com/Home/SendForm.
For giggles, if you hard-code the path without a leading slash, does it work?

e.g.

$.ajax({
                    url: 'Home/SendForm',
...

Open in new window

Avatar of flynny

ASKER

Ok I have installed and run fiddler and heres the RAW data

POST http://dev.mydomain.co.uk/Home/SendForm HTTP/1.1
Host: dev.mydomain.co.uk
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0
Accept: */*
Accept-Language: en-GB,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: application/json; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://dev.mydomain.co.uk/
Content-Length: 200
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

{"amount":"255000","duration":"12","security":"1025000","loan":"500000","securityType":"Residential","charge":"1st Charge","fullName":"matt","email":"matt@test.co.uk","phone":"1234567"}
Avatar of flynny

ASKER

@Kaufmed,

I hardcoded but it resolved the url to /Home/SendForm.

Is there anything I can check in the routing?

I did find this article

http://stackoverflow.com/questions/9703090/http-404-page-not-found-in-web-api-hosted-in-iis-7-5

but this change has made no difference (its still working locally with this change to the wb.config, just not on the server)
When you have that leading slash in there, which is what Url.Action is doing, it tells the server to look in its root, not the application's root. You can see this in your POST above. You need to either nix the leading slash, or you need to append the application name to the front of the url value.
Avatar of flynny

ASKER

Hi Kaufmed,

sorry could you explain further? I'm not fully understand what your saying here?
Does your application live in the root directory of your web server?

Can you post the relevant bit of code you are using to handle the request "/Home/Sendform" in your routes?
Avatar of flynny

ASKER

Hi,

Sorry yes the application is in the root, i.e. dev.mydomain.co.uk is where the site resides.

for rerefence here the routeconfig

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }

Open in new window

Avatar of flynny

ASKER

also (I hope it helps) heres the raw fiddler output from the localhost which is successful;

POST http://localhost:13071/Home/SendForm HTTP/1.1
Host: localhost:13071
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0
Accept: */*
Accept-Language: en-GB,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: application/json; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://localhost:13071/
Content-Length: 193
Cookie: _ga=GA1.1.551316189.1422270859
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

{"amount":"255000","duration":"12","security":"1025000","loan":"500000","securityType":"Residential","charge":"1st Charge","fullName":"matt","email":"matt@test.co.uk","phone":"1234567"}

Open in new window

I found the following with a bit of searching which may be relevant:

What I'd found on my setup of IIS7.5 is that the 'Handler Mapping' has a resource named 'OPTIONSVerbHandler' is not set in the right order hence return back as Unknown.

This work for me where my localhost ajax was calling my network server, which has a different name, that it shouldn't give me a CORS issue, but it did and this was my solution.

Open IIS and click on your server name on the left pane. On the right pane double-click 'Handler Mappings' in the middle pane. On the right pane, select 'View Ordered List'. From there find 'OPTIONSVerbHandler' and 'svc-ISAPI-4.0_32bit', move 'OPTIONSVerbHandler' up until it is above 'svc-ISAPI-4.0_32bit'.

Make sure your 'handler' inside your ajax call does not have 'Access-Control-Allow-Origin' in it.
SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America 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 flynny

ASKER

@Kaufmed,

Thanks for the explaination. The site is under the root directory of the dev.mydomain.co.uk.

so navigating to dev.mydomain.co.uk/Home/Index returns the home view. dev.mydomain.co.uk/Home/Contact is returnign the contact page etc.

I tried simply hardocding both 'SendForm' and 'Home/SendForm' again but this still gives the 404 error (and same results in fiddler).

@James,

thanks for the link I have moved the handler above the said svc-ISAPI etc but this is still giving the 404 error message.
Strange indeed.

Can you view the post in Firebug or Developer tools and post the response headers you're getting? I have a feeling its an IIS configuration issue.
Avatar of flynny

ASKER

James,

No problem and thanks again guys for taking the time to help me with this.

the raw request from firefox is

Host: dev.mydomain.co.uk
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0
Accept: */*
Accept-Language: en-GB,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: application/json; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://dev.mydomain.co.uk/
Content-Length: 189
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

Open in new window


and the raw response is

Cache-Control: private
Content-Length: 1916
Content-Type: text/html; charset=utf-8
Date: Tue, 03 Feb 2015 16:35:49 GMT
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET

Open in new window


does that shed any light on the issue?
No problem. What type of response are you expecting, JSON? XML?
Avatar of flynny

ASKER

its a basically posting a some info to the server which then sends an email returning a boolean.
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
Avatar of flynny

ASKER

James and Kaufmed,

sorry for the delay in coming back you. After tinkering I finally managed to get it working. Although I'm not entirely sure what was going on.

First of all I tried reuploading the entire site, which made no difference, so I then deleted the site from IIS, recreated it and reuploaded the site again and it magically started working?

From googling I'm not sure whether creating a new app pool maybe sorted this, but it workig anyway and I thought I'd share how it was fixed.

I'll split the points between you and once again, many thanks for your time and input.
Avatar of flynny

ASKER

Amazing support and fast, reliable and helpful comments.