문제

I work in VS2010, ASP.NET MVC 2 project. The project was completed, it remains to be revealed at the IIS, but the problems arise. AJAX request that calls the controller method does not work on IIS, but work in VS. I do not know whether something should be included especially for IIS support of AJAX? I need help and how to forward the request and url is interpreted as the MVC request with parameters? I use IIS 5.

도움이 되었습니까?

해결책

You have hardcoded the urls in your javascript, haven't you:

$.post('/home/index', function(result) {

});

instead of using URL helpers:

$.post('<%= Url.Action("index", "home") %>', function(result) {

});

The reason your code doesn't work when you deployed your application in IIS is because you now have a virtual directory, like so: http://example.com/myapplication/home/index. If you hardcode the url instead of using URL helpers to generate it you end up with an AJAX request to http://example.com/home/index which of course doesn't work as you are missing the application name.

When working locally there is no virtual directory if you are using Visual Studio's built-in web server and the url looks like this: http://locahost:1234/home/index.

Personally what I would recommend you is to simply use HTML helpers to generate DOM elements such as <form> and <a> and then unobtrusively AJAXify them.

For example you could have the following form:

<% using (Html.BeginForm("SomeAction", "SomeController")) { %>
    ... some input fields
<% } %>

which you could AJAXify like this:

$(function() {
    $('form').submit(function() {
        $.post(this.action, $(this).serialize(), function(result) {
            // TODO: process the results
        });
        return false;
    });
});

See how we are no longer hardcoding any urls in our javascript files? Now everything will work no matter where you host your application.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top