【问题标题】:Sending HTML data to controller via jQuery通过 jQuery 向控制器发送 HTML 数据
【发布时间】:2014-10-31 00:49:08
【问题描述】:

我正在使用summernote,我想将部分数据发布到控制器;这是数据的样子

<p>This is a sample test.&nbsp;</p>

下面是我正在使用的代码

var sHTML = $('#summernote').code();
    $.ajax({
        url: "Home/GetText",
        data: sHTML,
        type: 'POST',
        cache: false,
        contentType: false,
        processData: false,
        success: function (vaildText) {
            // So something
        },
        error: function () {
            alert('error');
            // handle error
        }
    }); 

家庭控制器

[HttpPost]
    [ValidateInput(false)] 
    public ActionResult GetText(string sHTML)
    {
        // doing something

        return Json(vaildText);
    }

但是家庭控制器上的 SHTML 给出了 NULL。我哪里错了?这是将 HTML 数据发布到控制器的正确方法吗?

Arun 请求的堆栈

    Request URL:http://localhost:23680/Home/GetText
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:15
Content-Type:text/plain;charset=UTF-8
Cookie:__AntiXsrfToken=9d34feb94ea546abb4f50eb7251b4e25
Host:localhost:23680
Origin:http://localhost:23680
Referer:http://localhost:23680/
User-Agent:Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.103 Safari/537.36
X-Requested-With:XMLHttpRequest
**Request Payload**
[object Object]

【问题讨论】:

  • 什么是 sHTML?变量是如何创建的
  • $('#summernote').code()返回的内容
  • @ArunPJohny

    这是一个示例测试。 

  • 你需要将值作为请求参数传递...所以试试下面的答案

标签: jquery asp.net-mvc-4 html-validation summernote


【解决方案1】:

您需要创建一个名为 sHTML 的请求参数并传递值

var sHTML = $('#summernote').code();
$.ajax({
    url: "Home/GetText",
    data: {
        sHTML: sHTML
    },
    type: 'POST',
    cache: false,
    contentType: false,
    processData: false,
    success: function (vaildText) {
        // So something
    },
    error: function () {
        alert('error');
        // handle error
    }
});

【讨论】:

  • 我的控制器仍然为空。我必须将字符串 sHTML 更改为其他内容还是我犯了其他错误?
  • 你能用浏览器的开发者工具检查请求,看看发送的参数是什么
  • 在控制台中我没有收到任何错误。我不知道如何检查开发人员工具上的参数(我现在正在谷歌搜索)
  • 这是我得到的请求 URL:localhost:23680/Home/GetText 请求方法:POST 状态代码:200 OK 请求标头查看源接受:*/* Accept-Encoding:gzip,deflate Accept-Language:en-US ,en;q=0.8 Connection:keep-alive Content-Length:15 Content-Type:text/plain;charset=UTF-8 Cookie:__AntiXsrfToken=9d34feb94ea546abb4f50eb7251b4e25 Host:localhost:23680 Origin:localhost:23680Referer:localhost:23680 User -Agent:Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.103 Safari/537.36 X-Requested-With:XMLHttpRequest Request Payload [object Object]
  • @Zerotoinfinite 不是 ASP.NET 人...您希望请求数据作为请求参数还是请求正文
【解决方案2】:

只需尝试删除 contentType: false 并在 ajax 调用中添加 datatype,如下所示:-

var sHTML = $('#summernote').code();
$.ajax({
  url: "Home/GetText",
  data: {
    sHTML: sHTML
  },
  type: 'POST',
  datatype:'JSON'
  cache: false,
  processData: false,
  success: function (vaildText) {
    // So something
  },
  error: function () {
    alert('error');
    // handle error
  }
});

【讨论】:

    猜你喜欢
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    相关资源
    最近更新 更多