MVC 3 JSON调用无法在IIS中运行

Alv*_*vos 5 iis ajax jquery json asp.net-mvc-3

我正在使用ASPX引擎的MVC 3应用程序,作为一个开始,我开发了一个简单的搜索,利用JQuery JSON调用来检索一些信息.该调用发送从文本输入中获取的参数,并使用结果更新表.功能看起来像这样:

        function PerformLookup() {
            var _accountNumber = $('#accountNumber').val();

            $.ajax({
                url: '/SearchAjax/SearchAccount',
                type: 'POST',
                data: '{_accountNumber:'+_accountNumber+'}',
                dataType: 'json',
                contentType: 'application/json; charset=utf-8',
                success: function (data) {
                    UpdateTable(data);
                },
                error: function () {
                    alert('An error occurred while performing the search.');
                }
            });

            return false;
        }
Run Code Online (Sandbox Code Playgroud)

服务器代码使用该参数运行查询,并返回一个序列化为JSON的列表,以便与JQuery一起正常工作.服务器代码如下所示:

        [HttpPost]
        public JsonResult SearchAccount(string _accountNumber)
        {
            MLIBEntities dbMLIB = new MLIBEntities();

            var searchResults = (from s in dbMLIB.Sets
                                 where s.setmap1 == _accountNumber
                                 select s);
            return Json(searchResults.ToList());
        }
Run Code Online (Sandbox Code Playgroud)

正如您所看到的那样,当我从VS2010运行项目并使用其虚拟机时,它非常有效.

当我在带有IIS 7的Windows 2008服务器中发布项目时,会出现问题.项目正常运行但是当我运行PerformLookup函数时,我收到消息"执行搜索时出错"意味着ajax调用失败.

有没有人知道为什么在VS2010虚拟机中完美运行时,IIS中的呼叫失败?我明智地错过任何配置IIS吗?

提前致谢!

Dar*_*rov 10

永远不要硬编码这样的网址,因为在部署应用程序时,可能会有一个虚拟目录添加到您的网址:

url: '/SearchAjax/SearchAccount',
Run Code Online (Sandbox Code Playgroud)

在处理网址时始终使用网址助手:

url: '<%= Url.Action("SearchAccount", "SearchAjax") %>',
Run Code Online (Sandbox Code Playgroud)

所以这就是我如何重构你的代码:

function PerformLookup() {
    var _accountNumber = $('#accountNumber').val();
    $.ajax({
        url: '<%= Url.Action("SearchAccount", "SearchAjax") %>',
        type: 'POST',
        data: JSON.stringify({ _accountNumber: _accountNumber }),
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            UpdateTable(data);
        },
        error: function () {
            alert('An error occurred while performing the search.');
        }
    });
    return false;
}
Run Code Online (Sandbox Code Playgroud)

或者如果PerformLookup在单击某个链接时调用此函数,我将使用HTML帮助程序生成链接:

<%= Html.ActionLink(
    "Perform search", 
    "SearchAccount", 
    "SearchAjax", 
    null, 
    new { id = "search" }
) %>
Run Code Online (Sandbox Code Playgroud)

然后简单地AJAX化它:

$(function() {
    $('#search').click(function() {
        var _accountNumber = $('#accountNumber').val();
        $.ajax({
            url: this.href,
            type: 'POST',
            // Probably no need to send JSON request
            // so I've replaced it with a standard
            // application/x-www-form-urlencoded POST request
            data: { _accountNumber: _accountNumber },
            dataType: 'json',
            success: function (data) {
                UpdateTable(data);
            },
            error: function () {
                alert('An error occurred while performing the search.');
            }
        });
        return false;
    });
});
Run Code Online (Sandbox Code Playgroud)

最后,我强烈建议您使用FireBug,这是一个很好的工具,允许您调试此类问题,因为它显示所有AJAX请求以及客户端和服务器之间发生的事情.

  • 我使用了第一个带有URL帮助器和JSON.stringify函数的重构器,它就像IIS中的一个魅力一样.非常感谢你,你肯定知道你在做什么. (2认同)