从模块DotNetNuke的呼叫阿贾克斯 [英] Dotnetnuke Call ajax from a module
问题描述
我现在正在试图建立使用AJAX调用的DNN模块。但有一个jQuery错误,指出
语法错误:意外标记<
我试图解决与阿贾克斯URL:,并试图建立在根文件夹的新的ascx,但仍然显示错误404
我的Ajax调用如下
$。阿贾克斯({
网址:NewsManagement.ascx /添加,
的contentType:应用/ JSON的;字符集= UTF-8,
数据类型:JSON,
方法:POST,
beforeSend:函数(){
},
缓存:假的,
数据: {
标题:$('#txt_Title)VAL()。
news_content:$('#txt_Content)VAL()。
图片:$('#file_Image)VAL()。
chapter_id:$('#sel_Chapter)VAL()
is_draft:$('#chk_Draft)VAL()。
posted_date:$('#dp_PostDate)VAL()。
由...制作 : ,
lastupdate_by:
},
成功:功能(数据){
的console.log(数据);
如果(数据==成功){
的console.log(数据);
}
其他 {
initMdlError(服务器:+数据);
}
},
错误:功能(数据,textStatus,错误){
//错误是被称为FROM HERE
的console.log(JAVASCRIPT JQUERY:+误差);
initMdlError(错误);
},
完成:函数(){
的console.log('完成');
}
});
&LT;脚本src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>$c$c>$p$p>
有没有什么办法可以解决这个问题?
您正在运行到的问题是,DNN不处理请求的URL正确,你在呼唤。如果你想调用的DNN服务的URL你会想设置路由处理来电。
命名空间Christoc.Com.Modules.Slide presentation.services
{
公共类幻灯片presentationRouteMapper:IServiceRouteMapper
{
公共无效的RegisterRoutes(IMapRoute mapRouteManager)
{
mapRouteManager.MapRoute(幻灯片presentation,{}控制器ashx的/ {行动},
新的[] {Christoc.Com.Modules.Slide presentation.services});
}
}
}
在控制器,你可以定义可用的方法
[DnnAuthorize(使用AllowAnonymous =真)
公众的ActionResult ListOfSlides()
{
尝试
{
VAR幻灯片= Slide.GetSlides(ActiveModule.TabID,ActiveModule.ModuleID);
返回JSON(幻灯片,JsonRequestBehavior.AllowGet);
}
赶上(例外EXC)
{
DnnLog.Error(极好);
返回JSON(空,JsonRequestBehavior.AllowGet);
}
}
<一个href="https://slide$p$psentation.$c$cplex.com/SourceControl/latest#DesktopModules/Slide$p$psentation/services/Slide$p$psentationController.cs" rel="nofollow">https://slide$p$psentation.$c$cplex.com/SourceControl/latest#DesktopModules/Slide$p$psentation/services/Slide$p$psentationController.cs
样品Javascript的
//获得初始化幻灯片
this.init =功能(元素){
// VAR数据= {}; //删除,因为我们不需要这个
//data.moduleId =的moduleId; //删除,因为我们调用setModuleHeaders时不需要这个
//data.tabId = tabId; //删除,因为我们不需要这个
//serviceFramework.getAntiForgeryProperty(); //删除,因为我们不需要这个
$阿贾克斯({
键入:POST,
缓存:假的,
网址:baseServicePath +ListOfSlides,
//数据:数据,
//数据类型:JSON,
beforeSend:serviceFramework.setModuleHeaders
})。完成(功能(数据){
viewModel.slides = ko.utils.arrayMap(数据,功能(多个){
返回新的幻灯片(S);
});
ko.applyBindings(视图模型);
$(元素).jm preSS();
}),失败(函数(){
CONSOLE.LOG('对不起加载失败幻灯片');
});
};
下面是做这个的一个例子模块
的https://slide$p$psentation.$c$cplex.com/
和用户组的视频我做年前的这个模块。 https://www.youtube.com/watch?v=hBqn5TsLUxA
I am now trying to build a dnn module using ajax calls. But there is a jquery error stating
SyntaxError: Unexpected token <
I have tried to work around with ajax "url: " and tried to create a new ascx at the root folder but still showing error 404.
My ajax call is as below
$.ajax({
url: "NewsManagement.ascx/Add",
contentType: "application/json; charset=utf-8",
dataType: "json",
method: "POST",
beforeSend: function () {
},
cache: false,
data: {
title : $('#txt_Title').val(),
news_content : $('#txt_Content').val(),
image : $('#file_Image').val(),
chapter_id : $('#sel_Chapter').val(),
is_draft : $('#chk_Draft').val(),
posted_date : $('#dp_PostDate').val(),
created_by : "",
lastupdate_by : ""
},
success: function (data) {
console.log(data);
if (data == "success") {
console.log(data);
}
else {
initMdlError("SERVER : " + data);
}
},
error: function (data, textStatus, error) {
// ERROR IS BEING CALLED FROM HERE
console.log("JQUERY JAVASCRIPT : " + error);
initMdlError(error);
},
complete: function () {
console.log('complete');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Is there any way to solve the issues?
The problem you're running into is that DNN isn't handling the requested URL properly that you are calling. If you want to call a service URL in DNN you're going to want to setup routes to handle the calls.
namespace Christoc.Com.Modules.SlidePresentation.services
{
public class SlidePresentationRouteMapper : IServiceRouteMapper
{
public void RegisterRoutes(IMapRoute mapRouteManager)
{
mapRouteManager.MapRoute("SlidePresentation", "{controller}.ashx/{action}",
new[] {"Christoc.Com.Modules.SlidePresentation.services"});
}
}
}
In the Controller you can define the methods available
[DnnAuthorize(AllowAnonymous = true)]
public ActionResult ListOfSlides()
{
try
{
var slides = Slide.GetSlides(ActiveModule.TabID, ActiveModule.ModuleID);
return Json(slides, JsonRequestBehavior.AllowGet);
}
catch (Exception exc)
{
DnnLog.Error(exc);
return Json(null, JsonRequestBehavior.AllowGet);
}
}
sample Javascript
//get slides on initialization
this.init = function(element) {
//var data = {}; //removed because we don't need this
//data.moduleId = moduleId; //removed because we don't need this when calling setModuleHeaders
//data.tabId = tabId; //removed because we don't need this
//serviceFramework.getAntiForgeryProperty(); //removed because we don't need this
$.ajax({
type: "POST",
cache: false,
url: baseServicePath + 'ListOfSlides',
//data: data,
//dataType:"json",
beforeSend: serviceFramework.setModuleHeaders
}).done(function(data) {
viewModel.slides = ko.utils.arrayMap(data, function(s) {
return new slide(s);
});
ko.applyBindings(viewModel);
$(element).jmpress();
}).fail(function () {
Console.Log('Sorry failed to load Slides');
});
};
Here's an example module that does this
https://slidepresentation.codeplex.com/
And a user group video I did years ago on this module. https://www.youtube.com/watch?v=hBqn5TsLUxA
这篇关于从模块DotNetNuke的呼叫阿贾克斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!