【问题标题】:JSON stringify not working over the IIS callJSON stringify 无法通过 IIS 调用
【发布时间】:2013-02-12 10:05:37
【问题描述】:

在我的 asp.net MVC 3 应用程序中。

以下 ajax 调用在我的开发机器上运行良好,但是当我在 IIS 服务器上发布应用程序时它失败或对象总是发送空值来保存函数。

        $.ajax
        ({
            url: '../MyPath/save',
            type: 'POST', 
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify({'Model': object}),
            success: function (data) {
                alert("success");
                return true;
            }
          });

    [HttpPost]
    public JsonResult Save(SampleModel Model) 
           * Model values always null over IIS *
    { 

    }

我什至尝试使用@url.action(),甚至尝试在另一个浏览器中,但仍然存在同样的问题。

任何人都知道为什么这不适用于 IIS 调用。?

请建议。谢谢

【问题讨论】:

    标签: asp.net-mvc model-view-controller jquery


    【解决方案1】:

    我认为你应该使用jQuery.parseJSON()

         $.ajax({
            url: '../MyPath/save',
            type: 'POST', 
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            data: jQuery.parseJSON({'Model': object}),
            success: function (data) {
                alert("success");
            }
         });
    

    来自文档:

    Passing in a malformed JSON string may result in an exception being thrown. 
    For example, the following are all malformed JSON strings: 
    
    • {test: 1}test 周围没有双引号)。
    • {'test': 1}'test' 使用的是单引号而不是双引号)。

    More info

    【讨论】:

    • 谢谢 jai ..我已经尝试过了,但仍然面临同样的问题..请建议另一种方法来做到这一点。我从 1 天开始就拉头发..仍然没有弄清楚这个问题..谢谢
    【解决方案2】:

    您的 JavaScript 将导致您的模型被双重包装,绑定过程将在 SampleModel 类中寻找 Model 属性。直接绑定应该是:

    $.ajax({
        url: '../MyPath/save',
        type: 'POST', 
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify(object),
        success: function (data) {
            alert("success");
        }
    });
    

    【讨论】:

    • 谢谢..但我也试过这个..不工作.plz建议另一种方式..仍然发送空值..
    • @user1799982 向我们展示您的 JavaScript 对象和 SampleModel 类
    猜你喜欢
    • 1970-01-01
    • 2013-12-27
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多