c# - 为什么当我使用 stringify 发布 html 时会发生此 ajax 错误

标签 c# javascript jquery vb.net

当我尝试使用 stringify 在字符串中发布 html 时,为什么会发生此 ajax 错误。

看起来 stringify 会自动转义字符。

我必须逃跑吗?谢谢

var s;

//s = "my test test"; //if I post this it works

s = "my test test<br />"; //if I post this it break when I add the html

        var a = { "myText": JSON.stringify(s) };

        $.ajax({
            type: "POST",
            url: "test.aspx",
            data: a,
            success: function (data) {

                  //pass

            },
            error: function () {
            alert("error");

            }
        });

然后在页面加载时我尝试使用

读取发布的数据
HttpContext.Current.Request.Form("myText")

最佳答案

试试这个:

var s = "my test test<br />",
a = { "myText": s };
a = JSON.stringify(a)
$.ajax({
    type: "POST",
    url: "test.aspx",
    data: a,
    success: function (data) {
          //pass
    },
    error: function () {
    alert("error");

    }
});

关于c# - 为什么当我使用 stringify 发布 html 时会发生此 ajax 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11907594/

相关文章:

javascript - Jquery .css() 禁用媒体查询所做的高度更改?

jquery - Rails,发送 json 参数

c# - HttpClient系统.Net.Http.HttpRequestException : Response status code does not indicate success: 401 (Unauthorized)

c# - 如何在不使用存储过程的情况下使用 C# 中的 MySQLCommand 创建安全查询以在 MySQL 中执行批量插入?

c# - 协变分配不起作用

c# - 为什么 ArrayList 实现 IList、ICollection、IEnumerable?

javascript - 如何将当前选项卡静音或阻止网站的声音?

javascript - javascript继承的这段代码有什么问题?

jquery - 使刷新功能仅刷新页面的某些部分

jquery - 用JQuery改变CSS中进度条的 'width'