0

我似乎无法从 Dot Net Nuke 站点内的 Ajax 帖子中获得 JSON 响应。它改为返回 HTML 作为响应。

我能够让它在一个正常的测试站点上正常工作,我想知道是否有人知道我需要做什么。

下面是我现在正在测试的代码:

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#函数:

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

就像我说的那样,这个确切的代码(除了不同的 URL)在普通的 .NET 站点中运行良好,但是当我将它移到 Dot Net Nuke 站点时,它返回 HTML。

有任何想法吗??

4

1 回答 1

1

DNN 的服务层允许您遵循类似 Webapi 的方法,我认为您会发现更容易控制传入/传出的数据。

这是开源文章模块的控制器示例 https://dnnsimplearticle.codeplex.com/SourceControl/latest#cs/services/DnnSimpleArticleController.cs

就像是

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?
            }
        }
于 2013-08-29T20:05:33.010 回答