jQueryからC#にajaxを使用してデータを送信したい(注:同じページにリクエストを送信しています)。私はこのチュートリアルhttp://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/に従っています。
注: これは、SharePoint 2010 Web パーツ ソリューション上の 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
メインファイルとは別のファイルにある関数と関係がありますか? それとも、私の URL が正しくないのでしょうか? 現在のページはaspxページであり、POSTを(同じページに)送信するためのjQueryコードも含まれているため、URLをハードコーディングしたくありません。
誰が何が間違っているのか知っていて、これを修正できますか?