2016-01-22 33 views
1

我試圖參照這個視頻教程: https://www.youtube.com/watch?v=PfnTJsUKxiY 也是這個博客: https://cmsview.wordpress.com/tag/sitecore-speak-for-beginners/獲得404錯誤調用Sitecore的ItemWebApi通過Ajax

他們這些教程的例子都發起Ajax調用的URL /api/sitecore/{controller}/{action}。它是重定向到該控制器操作的默認Sitecore行爲,還是我需要自行配置路由?正如教程所顯示的,我無法獲得這個網址。每次它是404錯誤。我正在使用Sitecore 8.

謝謝。

編輯:下面 是在我的控制器類的代碼

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 

namespace SwireCitygate.Web.Controllers 
{ 
    public class MembersBehaviourReportsController : Controller 
    { 
     // GET: Default 
     public ActionResult GetText() 
     { 
      string text = "Hello World"; 

      return Json(text, JsonRequestBehavior.AllowGet); 
     } 
    } 
} 

爲繼承System.Web.Mvc.Controller

和JS文件:

define(["sitecore"], function (Sitecore) { 
    var model = Sitecore.Definitions.Models.ControlModel.extend({ 
     initialize: function (options) { 
      this._super(); 

      this.set("output", null); 
      this.GetText(this); 
     }, 
     GetText: function (app) { 
      jQuery.ajax({ 
       type: "GET", 
       dataType: "json", 
       url: "/api/sitecore/MembersBehaviourReports/GetText", 
       cache: false, 
       success: function (data) { 
        app.set("output", data); 
       }, 
       error: function() { 
        console.log("There was an error in GetText() function!"); 
       } 
      }); 
     } 
    }); 

    var view = Sitecore.Definitions.Views.ControlView.extend({ 
     initialize: function (options) { 
      this._super(); 

      this.set("output", null); 
     } 
    }); 

    Sitecore.Factories.createComponent("MembersBehaviourReportsDataSource", model, view, ".sc-MembersBehaviourReportsDataSource"); 
}); 
+1

你使用的是什麼網址?您不應在他們的網址中使用字詞控制器,例如如果你有'MyCustomController'類,你應該使用'/ api/sitecore/mycustom/someaction'。 –

+0

我正好使用'/ api/sitecore/mycustom/someaction',但它不起作用 – jackielpy

+0

你可以發佈你的代碼嗎? – NikolaiDante

回答

0

畢竟,問題是由Sitecore的配置引起的。 ItemWebAPI未啓用。

<site name="website"> 
    <patch:attribute name="itemwebapi.mode">Off</patch:attribute> 
    <patch:attribute name="itemwebapi.access">ReadOnly</patch:attribute> 
    <patch:attribute name="itemwebapi.allowanonymousaccess">false</patch:attribute> 
</site> 

我將itemwebapi.mode設置爲StandardSecurity和itemwebapi.allowanonymousaccess爲true,然後它神奇地工作。

1

沒有看到你的代碼,我會說你應該確保你的控制器繼承自MVC控制器System.Web.Controller而不是WebApi控制器System.Web.Http.ApiController

如果您正在使用的WebAPI控制器,你需要安裝一個定製的路由,而不是使用/ API/Sitecore的/ ...

+0

添加到原始問題的代碼片段 – jackielpy