2016-03-31 89 views
2

頁面中有一個像HTML這樣的選項卡式面板,其中每個選項卡點擊都會顯示不同的數據。 試圖用Ajax調用完成此操作。
在Visual Studio項目中,我創建了一個web服務test.asmx,裏面的服務文件夾。所以上發佈,它被保存在wwwroot/MyApp/Website/Services/test.asmx如何在Sitecore中創建Web服務

asmx.cs

[System.Web.Script.Services.ScriptService] 
    public class test : System.Web.Services.WebService 
    { 
     [WebMethod] 
     public string HelloWorld() 
     { 
      return "Hello World"; 
     } 
    } 

ASCX

<asp:ScriptManager ID="_scriptManager" runat="server"> 
    <Services> 
    <asp:ServiceReference Path="/Services/test.asmx" /> 
    </Services> 
</asp:ScriptManager> 

<script type="text/javascript"> 
    jQuery(function ($) {  
     console.log("howdy"); //this does print 
     var Param1 = 'one'; 
     var Param2 = 'two'; 
     $.ajax({ 
      type: "GET", 
      url: "/Services/test.asmx/HelloWorld", 
      data:"", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      async: "true", 
      cache: "false", 
      success: function (msg) { 
       alert(msg.d); 
      }, 
      Error: function (x, e) { 
       alert("errrrr"); 
      } 
     }); 
    }); 
</script> 

在瀏覽網頁,有這種錯誤在瀏覽器控制檯。

GET http://MyApp/Services/test.asmx/HelloWorld 500(內部服務器錯誤 )jQuery.noconflict.js:16

  1. 這是撥打電話
  2. 是不是因爲prototype.js中的正確方法衝突(不再是!!)。我已經說過jQuery(function ($) { ...,但仍然.....

回答

4

我個人不會使用Web服務,因爲我不認爲您將有Sitecore上下文來執行Sitecore工作。我只是使用MVC路線,並使用它像一個Web API。

控制器

using System.Web.Mvc; 
using Bonfire.Kickfire.Analytics.Dto; 
using Bonfire.Kickfire.Analytics.Models; 

namespace Bonfire.Kickfire.Analytics.Controllers 
{ 
    public class VisitorController : Controller 
    { 

     [HttpGet] 
     public JsonResult VisitorDetailsJson() 
     { 
      var item = Sitecore.Context.Item; 
      var vi = new VisitorInformation(); 
      var trackerDto2 = vi.GetTrackerDto(); 

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

路線

using System.Web.Mvc; 
using System.Web.Routing; 
using Sitecore.Pipelines; 

namespace Bonfire.Kickfire.Analytics.Pipelines.Initialize 
{ 
    public class InitRoutes : Sitecore.Mvc.Pipelines.Loader.InitializeRoutes 
    { 
     public override void Process(PipelineArgs args) 
     { 
      RouteTable.Routes.MapRoute(
       "Profile", // Route name 
       "VisitorData", // URL with parameters 
       new {controller = "Visitor", action = "VisitorDetailsJSON"}, 
       new[] {"Bonfire.Kickfire.Analytics.Controllers"}); 
     } 
    } 
} 

補丁文件

<?xml version="1.0" encoding="utf-8"?> 
 
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> 
 
    <sitecore> 
 
    <pipelines> 
 
     <initialize> 
 
     <processor type="Bonfire.Kickfire.Analytics.Pipelines.Initialize.InitRoutes, Bonfire.Kickfire.Analytics" 
 
      patch:before="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeRoutes, Sitecore.Mvc']"/> 
 
     </initialize> 
 
     </pipelines> 
 
    </sitecore> 
 
</configuration>