【问题标题】:Dotnetnuke Call ajax from a moduleDotnetnuke 从模块调用 ajax
【发布时间】:2015-11-17 15:08:52
【问题描述】:

我现在正在尝试使用 ajax 调用构建一个 dnn 模块。但是有一个jquery错误说明

SyntaxError: Unexpected token

我尝试过使用 ajax "url:" 并尝试在根文件夹中创建一个新的 ascx,但仍然显示错误 404。

我的ajax调用如下

$.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>

有什么办法可以解决问题吗?

【问题讨论】:

    标签: javascript jquery ajax dotnetnuke dotnetnuke-7


    【解决方案1】:

    您遇到的问题是 DNN 没有正确处理您调用的请求 URL。如果你想在 DNN 中调用服务 URL,你需要设置路由来处理调用。

    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"});
            }
        }
    }
    

    在控制器中你可以定义可用的方法

    [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);
         }
    }
    

    https://slidepresentation.codeplex.com/SourceControl/latest#DesktopModules/SlidePresentation/services/SlidePresentationController.cs

    示例 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');
            });
        };
    

    这是一个执行此操作的示例模块

    https://slidepresentation.codeplex.com/

    还有一个我多年前在这个模块上制作的用户组视频。 https://www.youtube.com/watch?v=hBqn5TsLUxA

    【讨论】:

    • 谢谢。我现在明白了。 :)
    猜你喜欢
    • 1970-01-01
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 2011-01-09
    • 2011-02-17
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    相关资源
    最近更新 更多