Dotnetnuke Call ajax from a module

本秂侑毒 提交于 2019-12-02 06:53:57
Chris Hammond

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

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!