在DNN8中通过Ajax调用MVC操作时附加额外的HTML结果
我是DNN开发的新手 我在VisualStudio中创建了一个非常简单的模块——一个文本框和一个按钮 我只想通过单击按钮调用控制器中的操作,然后在文本框中显示返回结果 代码调用动作成功,但不确定为什么在结果中附加大量HTML信息 以下是控制器中的操作:在DNN8中通过Ajax调用MVC操作时附加额外的HTML结果,html,ajax,asp.net-mvc,action,Html,Ajax,Asp.net Mvc,Action,我是DNN开发的新手 我在VisualStudio中创建了一个非常简单的模块——一个文本框和一个按钮 我只想通过单击按钮调用控制器中的操作,然后在文本框中显示返回结果 代码调用动作成功,但不确定为什么在结果中附加大量HTML信息 以下是控制器中的操作: public ActionResult test1() { return Content("Return something"); } 以下是视图中的Ajax代码: $(document).ready(function () {
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信息?事实上,我不需要它
谢谢不幸的是,如DNN8 MVC中所述,还无法返回JsonResult。因此,我使用的解决方案是返回ActionResult(尽管函数返回Json): 在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!");
}
});
该操作可以返回JsonResult。但请注意,如果您只调用该函数,它将失败,ModuleContext上会出现null异常。您必须在ajax调用中包含以下标题:
headers: {
"ModuleId": @Dnn.ModuleContext.ModuleId,
"TabId": @Dnn.ModuleContext.TabId,
"RequestVerificationToken": $("input[name='__RequestVerificationToken']").val()
}
您可以找到完整的模块代码。这是DNN9中的一个工作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);
}
快乐编码:)将其更改为
returnjson(“returnsomething”,JsonRequestBehavior.AllowGet)代码>并使用dataType:'json',
Hi-Stephen。谢谢当我将数据类型设置为JSON时,Ajax获得了200状态,但运行了error:function(){alert(“Failed!”)语句,而不是success:function(data)。您是否将其更改为数据类型:“JSON”
?
$.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);
}