我想發送數據使用Ajax從jQuery到C#(注:我發送請求到同一頁面)。 我正在關注本教程http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/。獲取錯誤,當做一個jQuery AJAX函數的ASPX頁
注意:這是SharePoint 2010 webpart解決方案上的asp.net 3.5。
這是我的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;
}
,並在C#中,主文件爲PDFLibraryUserControl.ascx.cs
。這是具有Page_Load
功能的那個。
不過我還有一個類文件名爲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;
}
}
}
我希望得到一個警告說1234
,而是我得到這個:
是否有事可做與功能saveOrder
在另一個文件比主要的?或者,也許我的網址不正確?我不想在其中對網址進行硬編碼,因爲當前頁面是aspx頁面,並且還包含發送POST(到同一頁面)的jQuery代碼。
有誰知道什麼是錯的,可以解決這個問題嗎?