[英]Getting error when doing a jquery ajax function to a aspx page
I want to send data using ajax from jQuery to c# (note: I'm sending request to the same page). 我想使用ajax从jQuery向c#发送数据(注意:我正在向同一页面发送请求)。 I am following this tutorial http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/ .
我正在按照本教程http://encosia.com/using-jquery-to-direct-call-aspnet-ajax-page-methods/进行操作 。
NOTE: This is a asp.net 3.5 on SharePoint 2010 webpart solution. 注意:这是SharePoint 2010 Webpart解决方案上的asp.net 3.5。
This is my jquery code: 这是我的jQuery代码:
$.ajax({
type: "POST",
url: getURLWithoutQuery() + "/saveOrder",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg.d);
}
});
// return the url of the current page without any query parameters
function getURLWithoutQuery() {
return location.protocol + '//' + location.host + location.pathname;
}
and in c#, the main file is PDFLibraryUserControl.ascx.cs
. 在c#中,主文件是
PDFLibraryUserControl.ascx.cs
。 This is the one that has the Page_Load
function. 这是具有
Page_Load
函数的代码。
However I have another class file called SaveStructure.cs
: 但是,我还有另一个名为
SaveStructure.cs
类文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.Services;
namespace PDFLibrary.PDFLibrary
{
public partial class SaveStructure : Page
{
[WebMethod]
public static int saveOrder()
{
return 1234;
}
}
}
I expect to get an alert saying 1234
, but instead I get this: 我希望收到警报说
1234
,但是我得到了:
Does it have something to do with the function saveOrder
being on another file than the main one? 它与函数
saveOrder
是否与主文件saveOrder
另一个文件上有关系吗? Or maybe my URL is not correct? 还是我的网址不正确? I don't want to hardcode the URL in it, because the current page is the aspx page and also contains the jQuery code to send the POST (to the same page).
我不想对其中的URL进行硬编码,因为当前页面是aspx页面,并且还包含jQuery代码以发送POST(到同一页面)。
Does anyone know what's wrong and can fix this? 有谁知道这是怎么回事,可以解决此问题?
Putting page methods inside of a user control is a bad idea, because the biggest problem is that user controls are not directly accessible via script (read: jQuery) like .aspx
pages are. 将页面方法放在用户控件中是一个坏主意,因为最大的问题是用户控件不能像
.aspx
页面那样通过脚本(阅读:jQuery)直接访问。 If you are constrained to only putting this logic in a user control, then my recommendation is create a web service ( .asmx
) that will perform the logic the page method was going to do. 如果只限于将此逻辑放在用户控件中,那么我的建议是创建一个Web服务(
.asmx
),该服务将执行page方法将要执行的逻辑。 A web service is directly accessible via the jQuery .ajax()
method, like this: 可以通过jQuery
.ajax()
方法直接访问Web服务,如下所示:
Code from myService.asmx:
[WebMethod]
public static int saveOrder()
{
return 1234;
}
Code in SharePoint .aspx page:
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
type: "POST",
url: "myService.asmx/saveOrder",
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
success: handleSuccess,
error: handleError
});
});
function handleSuccess(data, status) {
// Do something with successful data retrieval
}
function handleError(xmlRequest) {
// Do something with error
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.