MVC 3 JSON调用不工作在IIS工作、MVC、JSON、IIS

2023-09-10 17:24:32 作者:风月不等闲

我工作在一个MVC 3应用程序ASPX引擎和作为一个点开始我开发了利用JQuery的JSON调用来检索一些信息的简单搜索。呼叫发送从文本输入所取的参数和更新表的结果。该funcion是这样的:

I'm working in an MVC 3 application with ASPX engine and as a point of start I developed a simple search that utilizes a JQuery JSON call to retrieve some info. The call sends a parameter taken from a text input and updates a table with the results. The funcion look like this:

        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;
        }

服务器code运行与参数的查询,并返回被序列化到JSON被使用jQuery正常工作清单。服务器code是这样的:

The server code runs a query with that parameter and returns a list that is serialized to JSON to be worked normally with JQuery. The server code looks like this:

        [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());
        }

正如你看到它没有什么困难的,它完美的作品,当我运行VS2010的项目并使用其虚拟机。

As you see it's nothing difficult and it works perfectly when I run the project from VS2010 and use its virtual machine.

当我在Windows 2008服务器,IIS 7,该项目正常运行,但是当我运行PerformLookup功能我得到的消息,这意味着AJAX调用失败执行搜索时出现的错误发布项目的问题发生

The problem happens when I publish the project in a Windows 2008 server with IIS 7. The project runs normally but when I run the PerformLookup function I get the message "An error occurred while performing the search" meaning that the ajax call failed.

没有人有任何想法,为什么呼叫失败在IIS而在VS2010虚拟机的正常使用?我缺少任何配置IIS明智?

Does anyone have any idea why the call is failing in the IIS while working perfectly in the VS2010 virtual machine? Am I missing any configuration IIS wise?

在此先感谢!

推荐答案

千万不要硬code这样的网址,因为当你部署应用程序有可能是ppended你的URL的虚拟目录$ P $:

Never hardcode urls like this because when you deploy your application there could be a virtual directory prepended to your urls:

url: '/SearchAjax/SearchAccount',

时总是使用URL处理使用URL助手:

Always use Url helpers when dealing with urls:

url: '<%= Url.Action("SearchAccount", "SearchAjax") %>',

因此​​,这里是我将如何重构你的code:

So here's how I would refactor your code:

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;
}

如果此 PerformLookup 函数被调用一些链接被点击的时候,我会用HTML辅助生成的链接:

or if this PerformLookup function is called when some link is clicked, I would have the link generated with an HTML helper:

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

,然后简单地AJAXify它:

and then simply AJAXify it:

$(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;
    });
});

最后,我会强烈建议您使用萤火这是一个很好的工具,允许你,因为它显示了所有的AJAX请求调试这样那样的问题及什么客户机和服务器之间发生

And finally I would strongly recommend you using FireBug which is an excellent tool allowing you to debug this kind of problems as it shows all AJAX requests and what's happening between the client and the server.

 
精彩推荐
图片推荐