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

moiiocjp  于 2023-08-04  发布在  jQuery
关注(0)|答案(2)|浏览(110)

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

qf9go6mv

qf9go6mv1#

您需要创建一个名为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
    }
});

字符串

goqiplq2

goqiplq22#

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

字符串

相关问题