2013-08-29 89 views
0

我似乎无法从Dot Net Nuke站点中的Ajax帖子获取JSON响应。它将返回HTML作为响应。Dot Net Nuke Ajax响应返回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。

任何想法??

+0

是其返回HTML或XML? –

+0

它正在返回HTML。它只是呈现页面本身的html –

+0

这是因为DNN不以这种方式处理webrequests,这只是DNN的另一个URL,因此它响应一个页面。您需要创建一个服务才能实现此目标(如下所述) –

回答

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? 
      } 
     }