首页 > 解决方案 > 尝试发送和返回原始 html 时,Ajax 调用不起作用

问题描述

我正在使用 Ajax 调用来调用 C# 方法,该方法通过删除不需要的 html 标记和属性来处理用户粘贴输入。

当我使用 html 格式(标签和属性)粘贴内容时,我的 Ajax 调用不起作用。您能否建议如何为这种情况定义 Ajax 参数(将原始 html 发送到服务器端代码并​​返回原始 html)?

看法:

@(Html.Kendo().Editor()
 .Name("myEditor")
 .PasteCleanup(p => p.Custom("myPasteCleanUp")
)

脚本:

function myPasteCleanUp(input) {
      var response = null;
      if (input != null) {
       $.ajax({
         type: "POST",
         url: '/Home/htmlTagCleanUp',
         data: { userInput: input },
         async: false,
         success: function (response) {
            input = response;
         },
       });
     }
    return input;
   }

控制器:

[HttpPost]
[AllowAnonymous]
public ActionResult htmlTagCleanUp(string userInput)
{
  userInput = userInput;
  return Content(userInput, "text/html");
}

标签: htmlajaxmodel-view-controlleractionresultcontenttype

解决方案


阻止您的 AJAX 调用的原因是您添加了 if 条件:

if (html != null) {
}

你能告诉我html变量是在哪里定义的吗?尽管如此,我认为您正在尝试检查输入的空值,如果我们在它应该工作的条件下用输入变量替换html :

if (input != null) {
}

推荐阅读