【问题标题】:Append additional HTML result in calling MVC action by Ajax in DNN8在 DNN8 中附加额外的 HTML 结果以通过 Ajax 调用 MVC 操作
【发布时间】:2016-09-19 23:10:48
【问题描述】:

我是 DNN 开发的新手。

我在 Visual Studio 中创建了一个非常简单的模块——一个文本框和一个按钮。

我只想通过点击按钮调用控制器中的动作,然后在文本框中显示返回结果。

代码调用动作成功,但不确定为什么在结果中附加大量 HTML 信息。

这是控制器中的操作:

public ActionResult test1()
{
    return Content("Return something");
}

这是视图中的 Ajax 代码:

$(document).ready(function () {
    $("#btnSub").click(function () {
        //alert(this.action);
        $.ajax({
            type:"GET",
            contentType:"application/text",
            url: "@Url.Action("test1", "Sky")",
            data:"",
            dataType: "text",

            success: function (data) { $("#txtResult").val(data); alert("Success!") },
            error:function(){alert("Failed!")}
    });
});
});

这是文本框中显示的结果:

谁能告诉我为什么返回 HTML 信息?其实我不需要。

谢谢

【问题讨论】:

  • 将其更改为return Json("Return something", JsonRequestBehavior.AllowGet); 并使用dataType: 'json',
  • 嗨,斯蒂芬。谢谢。当我将数据类型设置为 JSON 时,Ajax 得到 200 状态,但运行 error:function(){alert("Failed!") 语句而不是 success: function (data) 。
  • 你改成dataType: 'json'了吗?

标签: html ajax asp.net-mvc action


【解决方案1】:

不幸的是,如 DNN8 MVC unsupported features 中所述,尚无法返回 JsonResult。所以我使用的解决方案是返回一个ActionResult(虽然函数返回Json):

public ActionResult Test()
{
    return Json(new { success = true });
}

在 jquery 方面,我设置了 ajax 调用以将结果作为 html 接收。这样可以避免浏览器显示解析错误。最后,只需要去掉 html 部分,手动解析响应即可。不是很干净,但在 DNN 支持 JsonResult 之前我找到的唯一解决方案。

$.ajax({
    url: '@Url.Action("Index", "Contact")',
    type: 'POST',
    dataType: 'html',
    data: $('#contact-form input').serialize(),
    success: function (response) {
        jsonPart = response.substring(0, response.indexOf("<!DOCTYPE html>"));
        var data = JSON.parse(jsonPart);
        if (data.success) {
            alert("Great");
        }
    },
    error: function (jqXHR, textStatus, errorThrown) {
        alert("Error!");
    }
});

编辑:改进的解决方案 DNN8 现在支持 IMvcRouteMapper。然后,您可以在 RouteConfig.cs 中注册路由。完成后,您可以使用以下 URL 调用该函数:

/DesktopModules/MVC/ModuleName/Controller/Action

动作可以返回一个 JsonResult。但请注意,如果您只是调用该函数,它将失败并在 ModuleContext 上出现空异常。您必须在 ajax 调用中包含以下标头:

headers: {
   "ModuleId": @Dnn.ModuleContext.ModuleId,
   "TabId": @Dnn.ModuleContext.TabId,
   "RequestVerificationToken": $("input[name='__RequestVerificationToken']").val()
}

您可以找到模块完整代码here

【讨论】:

    【解决方案2】:

    这是 DNN 9 中的有效 ajax 调用。您不必使用 @urlaction 它会提供整个 html 以及数据。 dnn.getVar("sf_siteRoot", "/") + "DesktopModules/MVC/ModuleName/Controller/Action", 这可以解决问题,不要忘记添加标题,否则会抛出 500 错误。

    $.ajax({
    url: dnn.getVar("sf_siteRoot", "/") + 
    "DesktopModules/MVC/ModuleName/Controller/Action",
    type: 'POST',
    contentType: "application/json; charset=utf-8",
    dataType: 'json',
    data: "{ 'id':" + JSON.stringify(3543)+" }",
    headers: {
                "ModuleId": @Dnn.ModuleContext.ModuleId,
                "TabId": @Dnn.ModuleCon`enter code here`text.TabId,
                "RequestVerificationToken": 
                 $("input[name='__RequestVerificationToken']").val()
            },
    success: function (response) {
            debugger;
    },
    error: function (errmsg) {
        alert("Error!");
    }
    });
    

    你的控制器应该是

     [HttpPost]
        public ActionResult ActionName(int id)
        {            
            var data = id;
            return BuidJsonResult(true,data);
        }
    

    快乐编码:)

    【讨论】:

      猜你喜欢
      • 2017-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-01
      • 1970-01-01
      • 2022-07-15
      相关资源
      最近更新 更多