【问题标题】:Jquery ajax not post form with special characters in mvcJquery ajax不在mvc中发布带有特殊字符的表单
【发布时间】:2018-12-03 19:35:33
【问题描述】:

我正在尝试使用 jquery ajax 发布整个表单。这是我的 ajax 调用:

 $(document).ready(function() {
        $('#btnadd').click(function () {
                $.ajax({
                    type: "POST",
                    url: "@Url.Action("SaveUser", "UserMenu")",                       
                    data: $('#SaveUserForm').serialize(),                                           
                    datatype: "json",
                    traditional: true,
                    success: function(d) {
                        var r = JSON.parse(d);
                        if (r.Result === 'true') {
                            //Wohoo its valid data and processed.
                            alert('success');
                        } else {
                            alert('not success');                                
                        }
                    },
                    error: function() {
                    }
                });             
        });
    });

当我发布没有特殊字符的表单时效果很好,但是如果任何字段包含特殊字符(假设我有一个包含字符 的文本字段密码)它不会触发控制器操作结果。 我已经厌倦了很多关于这个问题的解决方案,但对我来说没有这个工作。

这是我的控制器 ActionResult 用于接收 ajax 请求:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<JsonResult> SaveUser(AdminUserPanel model)
    {
      //some code for save into database
     return Json(new { Result = true });
    }

我是否需要更改我的 Ajax 功能或任何额外的设置(webconfig 或其他)?

【问题讨论】:

  • 你的类有数据注释吗?
  • 浏览器的调试控制台有错误吗?
  • @Jasen 没有浏览器的控制台错误
  • @nurdyguy 是的,我愿意 '[DisplayName("Password")]' '[Required(ErrorMessage = "Please enter Password")]' '[DataType(DataType.Password)]' for password field其中可能包含特殊字符。
  • @Spectarion 非常感谢。有用。你拯救了我的一天。再次感谢。

标签: c# jquery ajax asp.net-mvc


【解决方案1】:

这是因为 ASP.NET 认为它是潜在的危险文本。 对特定字段使用 [AllowHtml] 属性

例子:

public class BlogEntry {
    public int UserId {get;set;}
    [AllowHtml] 
    public string BlogText {get;set;}
 }

或使用 [ValidateInput(false)] 进行整个操作

[HttpPost]
[ValidateInput(false)]
public ActionResult xyz(abc model)
{
///////////
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    相关资源
    最近更新 更多