jQuery ajax 在 Sharepoint 可视化 Web 部件中不起作用
Posted
技术标签:
【中文标题】jQuery ajax 在 Sharepoint 可视化 Web 部件中不起作用【英文标题】:jQuery ajax not working in Sharepoint Visual Web Part 【发布时间】:2018-11-29 16:05:11 【问题描述】:所以我试图在我的 Sharepoint 2013 Visual WebPart 中做 jQuery ajax 的基本示例:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
<script type = "text/javascript">
function ShowCurrentTime()
$.ajax(
type: "POST",
url: "SPtest.aspx/GetCurrentTime",
data: 'name: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" ',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function(response)
alert(response.d);
);
function OnSuccess(response)
alert(response.d);
</script>
<div>
Your Name :
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<input id="btnGetTime" type="button" value="Show Current Time"
onclick = "ShowCurrentTime()" />
</div>
在 VisualWebPart1UserControl.ascx.cs 中的服务器上
[System.Web.Services.WebMethod]
public static string GetCurrentTime(string name)
return "Hello " + name + Environment.NewLine + "The Current Time is: "
+ DateTime.Now.ToString();
而且它不起作用,如果在 chrome 中打开调试器,我可以看到它在服务器端抛出此错误 500:
Server Error in '/' Application.
Unknown web method GetCurrentTime.
Parameter name: methodName
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: Unknown web method GetCurrentTime.
Parameter name: methodName
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[ArgumentException: Unknown web method GetCurrentTime.
Parameter name: methodName]
System.Web.Script.Services.WebServiceData.GetMethodData(String methodName) +806855
System.Web.Handlers.ScriptModule.OnPostAcquireRequestState(Object sender, EventArgs eventArgs) +238
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136
System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +195
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +88
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.2623.0
我不知道出了什么问题。
【问题讨论】:
【参考方案1】:问题已回答here。
You cant add a WebMethod to a webpart. A webpart is a server control and outputs html to the page.
【讨论】:
以上是关于jQuery ajax 在 Sharepoint 可视化 Web 部件中不起作用的主要内容,如果未能解决你的问题,请参考以下文章
SharePoint Web 部件:无法对 ashx 处理程序进行 jquery ajax 调用
Jquery(Ajax) 调用 SharePoint 2013 Search Rest API 并使用Josn反回结果并简单显示
Sharepoint 2010 在 jquery/javascript 中捕获 webpart 刷新
在我无法控制的任何 AJAX 调用之后使用 JQuery 修改 DOM