Search code examples
ajaxdotnetnuke

Dot Net Nuke Ajax Response Returning HTML


I can't seem to get a JSON response from an Ajax post within a Dot Net Nuke site. It returns HTML as a response instead.

I was able to get this to work in a normal test site just fine and am wondering if anybody may know what I need to do.

Below is the code I'm testing with for now:

JavaScript:

$("#ClearTaxFormButton").click(function (e) {
        e.preventDefault();
        var testValue = 7;

        $.ajax({
            type: "GET",
            url: "localhost/mywebsite/tabid/100/Default.aspx/SumbitByAjaxTest",
            data: '{ "taxRate":' + testValue + '}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                // Replace the div's content with the page method's return.
                //$("#Result").text(msg.d);
                alert(msg.d);
            }
        });
    });

C# Function:

//just using ths for testing
[WebMethod]
public static string SumbitByAjaxTest(string taxRate)
{
    return taxRate;
}

Like I said, this exact code (aside from a different URL) works fine in a normal .NET site but when I move it over to the Dot Net Nuke site, it returns HTML.

Any ideas??


Solution

  • DNN's service layer allows you to follow a Webapi like approach, I think you'll find that easier for controlling the data to/from.

    Here's an example of a controller for an open source articles module https://dnnsimplearticle.codeplex.com/SourceControl/latest#cs/services/DnnSimpleArticleController.cs

    Something like

    public HttpResponseMessage GetAllArticles(int portalId, bool sortAsc)
            {
                try
                {
                    //todo: get the latest X articles?
                    var articles = ArticleController.GetAllArticles(portalId, sortAsc);
    
                    //because of the circular reference when cerealizing the taxonomy within content items we have to build out our article view models manually.
                    var cleanArticles = new List<ArticleViewModel>();
                    foreach (Article a in articles)
                    {
                        var newArt = new ArticleViewModel
                        {
                            ArticleId = a.ArticleId,
                            Body = WebUtility.HtmlDecode(a.Body),
                            CreatedByUser = a.CreatedByUser,
                            CreatedByUserId = a.CreatedByUserId,
                            CreatedOnDate = a.CreatedOnDate,
                            Description = WebUtility.HtmlDecode(a.Description),
                            LastModifiedByUser = a.LastUpdatedByUser,
                            LastModifiedByUserId = a.LastModifiedByUserId,
                            LastModifiedOnDate = a.LastModifiedOnDate,
                            ModuleId = a.ModuleId,
                            Title = a.Title,
                            url = DotNetNuke.Common.Globals.NavigateURL(a.TabID, "", "&aid=" + a.ArticleId)
                        };
                        cleanArticles.Add(newArt);
                    }
    
                    var articleViewModels = new ArticleViewModels
                    {
                        Articles = cleanArticles
                    };
    
                    return Request.CreateResponse(HttpStatusCode.OK, articles);
    
                }
                catch (Exception exc)
                {
                    DnnLog.Error(exc); //todo: obsolete
                    return Request.CreateResponse(HttpStatusCode.BadRequest, "error in request"); //todo: probably should localize that?
                }
            }