locked
ajax script not working on IIS 7.5 RRS feed

  • Question

  • User881189886 posted

    I have a MVC 5 web app that runs fine under VS 2013 on my development server, but once I published it on IIS 7.5 win 2008 R2 the functions in my custom script file that call ajax stop working (goes right into the error function), although all other calls to jquery are still functioning properly. Is there anything else that needs to be done? I've looked at some post but couldn't find a solution yet.

    Monday, February 3, 2014 12:44 PM

Answers

  • User-1454326058 posted

    Hi cbenacscttech,

    As you said “The error code returned is 404 and the status = "Error"”, so the Ajax script is working.

    Please try to get the action URL using this code below:

    @Url.Action("GetRates", "Home")

    For your code:

    $(function () {
        $('#reservSearch').submit(function () {            // PartialView in the Index page: reservSearch.cshtml contains a Form with 2 controls: DropOff date and Pickup date + submit button 
            if ($(this).valid()) {
                $("#theModal").modal("show");              // Display the in progress.....
                $.ajax({
                    url: '@Url.Action("GetRates", "Home")',                 // URL for the request  This is an ActionResult in the Home controller that retrieves the rates and display partialView rates.cshtml
                    data: $("#reservSearch").serialize(),  // the data to send (will be converted to a query string)
                    type: "POST",                          // whether this is a POST or GET request  
                    dataType: 'html',                      // the type of data we expect back   
                    success: function (data) {             // code to run if the request succeeds; The response is passed to the function
                        $("#theModal").modal("hide");      // Close the in progress modal.....
                        $('#ratesView').html(data);        // Fill div with results: rates.cshtml is the partial view in the Index page that display the rates.
                    },
                    error: function (xhr, status) {        // Code to run if the request fails; the raw request and status codes are passed to the function
                        $("#theModal").modal("hide");      // Close the in progress modal.....
                        alert(xhr.error);
                    }
                });
            }
            // it is important to return false in order to cancel the default submission of the form and perform the AJAX call
            return false;
        });
    });

    Thanks

    Best Regards  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 5, 2014 8:37 PM

All replies

  • User1815411410 posted

    have you made sure ASP.NET MVC 5 is installed on your production server?

    As far as I am concerned, ASP.NET MVC 5 and AJAX should work on Windows Server 2008 R2. 

    I have tried it once and it works.

    I suggest that the issue is with the ASP.NET MVC 5 installed on your server and you may try to reinstall it to see whether it makes any differences

    Monday, February 3, 2014 11:00 PM
  • User881189886 posted

    I don't have ASP.NET MVC 5 installed on the server, nor could I find any related link to install it (bing). The web site however does work as expected, execept for this failed call to this ajax function. I've limited experience with IIS 7 and Ajax. I've read some posts about this problem, and most of them mention installing ajax ToolKit , adding lines to the we.config, and so on. But I think this is for older versions, although I'm not sure. If you can tell me with a considerable level of certainty that you can call a simple ajax on a 2008 R2 / IIS 7 without any additional requirements I will accept your answer. You also said you have tried it once and it worked. Would you by any chance have a sample of the function? I'm begining to suspect the problem may not be the call to ajax, but the way the function runs. Why it works locally and fails in the remote serve is still to be determined.

    Tuesday, February 4, 2014 10:49 AM
  • User1815411410 posted

    if it does not work, can you please post the error message here?

    Tuesday, February 4, 2014 11:01 PM
  • User-1454326058 posted

    Hi cbenac,

    If you can tell me with a considerable level of certainty that you can call a simple ajax on a 2008 R2 / IIS 7 without any additional requirements I will accept your answer

    Does it work with the simple Ajax function?

    the functions in my custom script file that call ajax stop working (goes right into the error function

    Do you use the Ajax request? If so, I suggest that you could use the Developer Tool (F12) to check the status of the request.

    On the other hand, please provide the Ajax function to us.

    Thanks

    Best Regards  

    Wednesday, February 5, 2014 12:49 AM
  • User881189886 posted
    This is the ajax function that is located in the  custom script. It works as expected in the local machine but fails in the remote server.
    The error code returned is 404 and the status = "Error". I know 404 is "Page not found" but all views are there. After the error it is possible to display the rates partial view


    // Retrieve rates and update the rates partial view $(function () { $('#reservSearch').submit(function () { // PartialView in the Index page: reservSearch.cshtml contains a Form with 2 controls: DropOff date and Pickup date + submit button if ($(this).valid()) { $("#theModal").modal("show"); // Display the in progress..... $.ajax({ url: '/Home/GetRates', // URL for the request This is an ActionResult in the Home controller that retrieves the rates and display partialView rates.cshtml data: $("#reservSearch").serialize(), // the data to send (will be converted to a query string) type: "POST", // whether this is a POST or GET request dataType: 'html', // the type of data we expect back success: function (data) { // code to run if the request succeeds; The response is passed to the function $("#theModal").modal("hide"); // Close the in progress modal..... $('#ratesView').html(data); // Fill div with results: rates.cshtml is the partial view in the Index page that display the rates. }, error: function (xhr, status) { // Code to run if the request fails; the raw request and status codes are passed to the function $("#theModal").modal("hide"); // Close the in progress modal..... alert(xhr.error); } }); } // it is important to return false in order to cancel the default submission of the form and perform the AJAX call return false; }); });

    Wednesday, February 5, 2014 9:25 AM
  • User-1454326058 posted

    Hi cbenacscttech,

    As you said “The error code returned is 404 and the status = "Error"”, so the Ajax script is working.

    Please try to get the action URL using this code below:

    @Url.Action("GetRates", "Home")

    For your code:

    $(function () {
        $('#reservSearch').submit(function () {            // PartialView in the Index page: reservSearch.cshtml contains a Form with 2 controls: DropOff date and Pickup date + submit button 
            if ($(this).valid()) {
                $("#theModal").modal("show");              // Display the in progress.....
                $.ajax({
                    url: '@Url.Action("GetRates", "Home")',                 // URL for the request  This is an ActionResult in the Home controller that retrieves the rates and display partialView rates.cshtml
                    data: $("#reservSearch").serialize(),  // the data to send (will be converted to a query string)
                    type: "POST",                          // whether this is a POST or GET request  
                    dataType: 'html',                      // the type of data we expect back   
                    success: function (data) {             // code to run if the request succeeds; The response is passed to the function
                        $("#theModal").modal("hide");      // Close the in progress modal.....
                        $('#ratesView').html(data);        // Fill div with results: rates.cshtml is the partial view in the Index page that display the rates.
                    },
                    error: function (xhr, status) {        // Code to run if the request fails; the raw request and status codes are passed to the function
                        $("#theModal").modal("hide");      // Close the in progress modal.....
                        alert(xhr.error);
                    }
                });
            }
            // it is important to return false in order to cancel the default submission of the form and perform the AJAX call
            return false;
        });
    });

    Thanks

    Best Regards  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 5, 2014 8:37 PM
  • User881189886 posted

    Starain Chen,

    I replaced the URL for the @Url.Action("GetRates", "Home") but it still not working.  With this URL.Action it also doesn't work in the local server. However, I think you were able to figure out exactly where the problem is. It seems to be exactly on that line of code. It would explain why the app runs in the development server (Under VS 2013) and not in the production: It has to be the path. It is the only thing that changes. Using the debugger, I was able to find out that the script runs but the GetRates() action is never called in the production server. 

    Since we now know that the Ajax works, I hope you can find a solution based on this new scenario.

    (By the way I was able to fix the 404 error. It was an incorrect call to the ErrorView page in the controller)

    Thanks so much.

    Wednesday, February 5, 2014 10:50 PM
  • User-1454326058 posted

    Hi cbenacscttech,

    By the way I was able to fix the 404 error. It was an incorrect call to the ErrorView page in the controller

    What the current status and URL of the AJAX request if you are using the @Url.Action method? (Using the Developer tool to check)

    I was able to find out that the script runs but the GetRates() action is never called in the production server

    How do you find out the GetRates action is never called?

    On the other hand, please provide the detail code of the GetRates action.

    Thanks

    Best Regards  

    Thursday, February 6, 2014 2:13 AM
  • User881189886 posted
    Starain Chen,

    I was able to fix the @Url.Action problem and now it looks in the correct path and all works as espected.
    Thank you

    Thursday, February 6, 2014 5:35 PM