通过jQuery将HTML数据发送到控制器

时间:2014-09-06 03:30:26

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

我正在使用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]

2 个答案:

答案 0 :(得分: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
    }
});

答案 1 :(得分:0)

尝试删除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
  }
});