页面中有一个类似于 HTML 的选项卡式面板,每个选项卡单击都会显示不同的数据。
尝试通过 Ajax 调用来完成此任务。
在 Visual Studio 项目中,我在 Services 文件夹内创建了一个 Web 服务 test.asmx。因此,在发布时,它保存在 wwwroot/MyApp/Website/Services/test.asmx
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 (Internal Server Error) jQuery.noconflict.js:16
- 这是正确的调用电话方式吗
- 是不是因为prototype.js冲突(又不是!!)。我已经说过
jQuery(function ($) {
...,但仍然...
最佳答案
就我个人而言,我不会使用 Web 服务,因为我认为您不会拥有正确的 Sitecore 上下文来执行 Sitecore 工作。我只想使用 MVC 路由并像 Web API 一样使用它。
Controller
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>
关于asp.net - 如何在 Sitecore 中创建 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36336878/