2014-01-24 71 views
0

我正在寫一個自定義Web部件到SharePoint 2013中。我需要創建一個ajax調用。我是多麼想 ......如何在Sharepoint 2013中創建ajax調用2013

在我CafeteriaWebPart.ascx,我寫

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %> 
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %> 
<%@ Import Namespace="Microsoft.SharePoint" %> 
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CafeteriaWebPart.ascx.cs" Inherits="RobiCafeteria.CafeteriaWebPart.CafeteriaWebPart" %> 

<div> 
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> 

<script type="text/javascript"> 
    $(document).ready(

     function() { 
      alert('Yes'); 
      $.ajax({ 
       type: "POST", 
       url: "CafeteriaWebPart.ascx.cs/GetCafeteriaList", 
       contentType: "application/json; charset=utf-8", 
       data: {}, 
       dataType: "json", 
       success: function (data) { 
        alert(data); 
       } 
      }); 
     } 
     ); 
</script> 
</div> 

,在我CafeteriaWebPart.ascx.cs,我寫

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Linq; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.Services; 
using Microsoft.SharePoint; 



namespace RobiCafeteria.CafeteriaWebPart 
{ 
    [ToolboxItemAttribute(false)] 
    public partial class CafeteriaWebPart : WebPart 
    { 
     // Uncomment the following SecurityPermission attribute only when doing Performance Profiling on a farm solution 
     // using the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready 
     // for production. Because the SecurityPermission attribute bypasses the security check for callers of 
     // your constructor, it's not recommended for production purposes. 
     // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)] 
     public CafeteriaWebPart() 
     { 
     } 

     protected override void OnInit(EventArgs e) 
     { 
      base.OnInit(e); 
      InitializeControl(); 
     } 

     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 

     [WebMethod] 
     public List<CafeteriaModel> GetCafeteriaList() 
     { 
      var spCafeteriaList = SPContext.Current.Web.Lists["Cafeteria"]; 

      return (from SPItem item in spCafeteriaList.Items 
        select new CafeteriaModel 
           { 
            DayName = item["Day Name"].ToString(), MenuItem = item["Menu Item"].ToString() 
           }).ToList(); 
     } 

    } 

    public class CafeteriaModel 
    { 
     public string DayName { get; set; } 
     public string MenuItem { get; set; } 
    } 
} 

我得到以下錯誤

POST http://srv-sptest:31618/SitePages/CafeteriaWebPart.ascx.cs/GetCafeteriaList 404 (NOT FOUND) 

回答

0

該網址應該是您網頁的網址後跟名稱你想調用的方法。例如:

url: "home.aspx/GetCafeteriaList", 
0

您需要在aspx頁面而不是ascx中定義web方法。它不能在用戶控制中工作。

您需要將url設置爲page.aspx/YourMethod。

相關問題