2013-08-29 87 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? 
      } 
     }