11

I have a web app that works fine on VS 2013 on my development server, but once I published it on the IIS 7.5 2008 R2 server the Ajax scripts, located on my custom script file, don't work anymore, although the other JQuery scripts that do not call Ajax do work properly. Is there anything else that needs to be done in order to have ajax working in the server? I've read some posts about, but couldn't find an answer yet. I've limited experience in IIS and Ajax.

// Update:

I've already figured out that the Ajax script works and that the problem is most likely in the following line:

"url: '/Home/GetRates', // URL for the request"

Using the debuger I found out that the GetRates() function is not been called in the Remote Server, although it is in the local (Under VS 2013) development server. The only difference that I see is the path, but dont know how to fix it. Below is the Ajax script:

// Retrieve rates and update partial view
$(function () {
    $('#reservSearch').submit(function () {
        if ($(this).valid()) {
            $("#theModal").modal("show");              // Display the in progress.....
            $.ajax({
                url: '/Home/GetRates',                 // URL for the request 
                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
                },
                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('Error: Retrieving parking rates' + "</br>" + 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;
    });
});

// SECOND UPDATE

After following the directions in the comment section, this is the response from the ajax call:

    <div id="header"><h1>Server Error in Application "DEFAULT WEB SITE"</h1></div> 
<div id="server_version"><p>Internet Information Services 7.5</p></div> 
<div id="content"> 
<div class="content-container"> 
 <fieldset><legend>Error Summary</legend> 
  <h2>HTTP Error 404.0 - Not Found</h2> 
  <h3>The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.</h3> 
 </fieldset> 
</div> 
<div class="content-container"> 
 <fieldset><legend>Detailed Error Information</legend> 
  <div id="details-left"> 
   <table border="0" cellpadding="0" cellspacing="0"> 
    <tr class="alt"><th>Module</th><td>IIS Web Core</td></tr> 
    <tr><th>Notification</th><td>MapRequestHandler</td></tr> 
    <tr class="alt"><th>Handler</th><td>StaticFile</td></tr> 
    <tr><th>Error Code</th><td>0x80070002</td></tr> 

   </table> 
  </div> 
  <div id="details-right"> 
   <table border="0" cellpadding="0" cellspacing="0"> 
    <tr class="alt"><th>Requested URL</th><td>http://localhost:80/Home/GetRates</td></tr> 
    <tr><th>Physical Path</th><td>C:\inetpub\wwwroot\Home\GetRates</td></tr> 
    <tr class="alt"><th>Logon Method</th><td>Anonymous</td></tr> 
    <tr><th>Logon User</th><td>Anonymous</td></tr> 

   </table> 
   <div class="clear"></div> 
  </div> 
 </fieldset> 
</div> 
<div class="content-container"> 
 <fieldset><legend>Most likely causes:</legend> 
  <ul>  <li>The directory or file specified does not exist on the Web server.</li>  <li>The URL contains a typographical error.</li>    <li>A custom filter or module, such as URLScan, restricts access to the file.</li> </ul> 
 </fieldset> 
</div> 
<div class="content-container"> 
 <fieldset><legend>Things you can try:</legend> 
  <ul>  <li>Create the content on the Web server.</li>  <li>Review the browser URL.</li>    <li>Create a tracing rule to track failed requests for this HTTP status code and see which module is calling SetStatus. For more information about creating a tracing rule for failed requests, click <a href="http://go.microsoft.com/fwlink/?LinkID=66439">here</a>. </li> </ul> 
 </fieldset> 
</div> 


<div class="content-container"> 
 <fieldset><legend>Links and More Information</legend> 
  This error means that the file or directory does not exist on the server. Create the file or directory and try the request again. 
  <p><a href="http://go.microsoft.com/fwlink/?LinkID=62293&amp;IIS70Error=404,0,0x80070002,7601">View more information &raquo;</a></p> 

 </fieldset> 
</div> 
</div> 
</body> 
</html> 
Ben Junior
  • 2,449
  • 10
  • 34
  • 51
  • AJAX requests are merely HTTP requests for a specified URL. Do these URLs still exist? If you can visit it in the web browser then AJAX can find it. – MonkeyZeus Feb 03 '14 at 17:11
  • @MonkeyZeus Yes, the URLs still exist. Is there any other requirement such as installing Ajax in the server/ IIS 7 or something in the web.config? Or this is not necessary? Your comment seems to be correct, cause the error returned is 404 (Page not found) although, as I checked, all views are there and they do display even after the error, which, by the way, is displayed in "error: function (xhr, status)" and not in the browser. – Ben Junior Feb 05 '14 at 14:43
  • AJAX is a client-side technology which has been supported in web browsers as far back as IE6. In essence a web server is not able to distinguish an AJAX request compared to you physically visiting the web page so there really shouldn't be any configuration required in IIS nor Apache. jQuery has simply taken it upon themselves to provide an easy-to-use wrapper for the native JavaScript **[XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest)** functions and give you a `$.ajax()` interface. Continued in next comment... – MonkeyZeus Feb 05 '14 at 18:04
  • Modern web browsers have made it incredibly easy to debug AJAX failures. What web browser do you use? If Google Chrome is available to you then please tell me and use it because I like their interface the best. I await your response – MonkeyZeus Feb 05 '14 at 18:06
  • @MonkeyZeus: I've updated the question. Please take a look at it. You were right from the begin; there was an incorrect way to call the ErrorView.cshtml in the controller, I fixed it and now the script is running, although not returning the data. I have the latest versions of both IE and Chrome and can use either. – Ben Junior Feb 06 '14 at 04:09
  • I see the update. |1) Go to the web page which makes the AJAX call |2) In Chrome press F12 |3) Go to the Network tab |4) Activate the AJAX call by submitting the form `#reservSearch` |5) In the Network tab look for a call to `/Home/GetRates` |6) Click it |7) Check the Preview and Response tabs to see the output from your server. Is it displaying the expected HTML data which your AJAX call is listening for? – MonkeyZeus Feb 06 '14 at 13:33
  • @MonkeyZeus: After following the directions in the comment above, this is the response from the ajax call. It points to a path that is not the one where the app is installed ("C:WepApps\AutoPark") Pls see more details in the second update in the question. Physical PathC:\inetpub\wwwroot\Home\GetRates – Ben Junior Feb 06 '14 at 22:23
  • Based on the error message, your web server cannot resolve `http://localhost:80/Home/GetRates` to any sort of file. In Apache there is something called `mod_rewrite` so I think your IIS `mod_rewrite` is busted. I use PHP and my URL would normally be `www.mysite.com/index.php/aClassName/aFunctionFromTheClass` so the `index.php` file handles the calls to that class structure and `mod_rewrite` makes it pretty like this `www.mysite.com/aClassName/aFunctionFromTheClass` – MonkeyZeus Feb 07 '14 at 00:27
  • Maybe this StackOverflow question can lead you in the right direction: http://stackoverflow.com/questions/5408111/codeigniter-2-on-iis-with-web-config-file – MonkeyZeus Feb 07 '14 at 00:28
  • @MonkeyZeus: I've fixed the problem by changing the url:"GetRates/Home" to url: '@Url.Action("GetRates", "Home"). This way it looked in the correct path. Thanks for all the orientation and specially on how to view the results. Please post a comment in an answer so that I can close this question – Ben Junior Feb 07 '14 at 00:35
  • That is excellent to hear! I knew you would get it working after some debugging. – MonkeyZeus Feb 07 '14 at 01:35

1 Answers1

40

How to debug AJAX calls

The full answer is spread across the comments on OP's question but I think this one helped the most:

  1. Go to the web page which makes the AJAX call
  2. In Chrome press F12
  3. Go to the Network tab
  4. Activate the AJAX call by submitting the form #reservSearch
  5. In the Network tab look for a call to /Home/GetRates
  6. Click it
  7. Check the Preview and Response tabs to see the output from your server
  8. Is it displaying the expected HTML data which your AJAX call is listening for?
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77