在IIS7中托管时,ASP.Net MVC $ .Ajax()函数无法正常工作

时间:2010-03-22 11:36:02

标签: jquery

我有一个ASP.Net MVC视图,其中显示了项目类别列表。当按下提交按钮时,我使用$ .Ajax()函数发布此表单。我在JSON中得到了结果(类别名称和描述)。当我从Visual Studio 2008运行时,此应用程序正常工作。但是当应用程序在IIS7中托管时,Ajax调用无效(成功:函数未调用)。

<script type="text/javascript">

    $(document).ready(function() {

        $('#JsonButton').click(function() {
            getDetails();
        });

        function getDetails() {
            $.ajax(
            {
                type: "POST",
                //url: "Home/GetDetailsInJson?categoryDropBoxId=" +                       $('#categoryDropBoxId').val() + "",
                url: "Home/GetDetailsInJson",
                data:
                {
                    "categoryDropBoxId": $('#categoryDropBoxId').val()
                },
                //contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(response) {

                    //alert($('#categoryDropBoxId').val());
                    $('#categoryDetails').empty();

                    var strHTML = '<fieldset>' +
                                    '<legend>CATEGOTY  DETAILS</legend>' +
                                    '<p>' +
                                    '<strong> Category Name: </strong>' + response.CategoryName +
                                    '</p>' +
                                    '<p>' +
                                    '<strong>Category Description: </strong>' + response.CategoryDescription +
                                    '</p>' +
                                    '</fieldset>'
                    //alert(strHTML);
                    $('#categoryDetails').append(strHTML);
                },

                failure: function(msg) {
                    alert(msg);
                    $('#categoryDetails').text(msg);
                }

            }); //end of $.ajax



        } //end of getDetails function
    });

</script>  

<%using (Ajax.BeginForm("Details", new AjaxOptions { UpdateTargetId = "categoryDetails" }))
       { %>

       <div>                 

           <table  width ="100%" >

                <tr >
                   <td>
                       <b>Category Details WCF Service ,View Model,Json &  $.ajax() call</b>
                   </td>
                   <td>
                        <input type="button" id="JsonButton" value="Get Details" />
                   </td>
               </tr> 
           </table>              
        </div>    
    <%} %>

    <div id="categoryDetails">

    </div   

2 个答案:

答案 0 :(得分:0)

在Visual Studio主机中,应用程序在Web“root”中运行。这可能不是IIS的情况。因此,您可能需要在方法中重写URL以考虑虚拟根。

您是否在本地计算机上的IIS7中运行应用程序?如果将项目设置切换到IIS,则可以托管它并仍然可以轻松地调试它。

答案 1 :(得分:0)

我的错误有点相同。但我忘了在C#脚本标签中包含脚本标签,如下所示:

    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" />

因此无法找到不显眼的部分。 也许这有助于有人来这里寻求帮助:)。