通过调用jQuery的ASP.NET服务器端方法 [英] Calling an ASP.NET server side method via jQuery
问题描述
我想通过jQuery来调用客户端服务器端方法。我的code是如下:
I'm trying to call a server side method from client side via jQuery. My code is as follows:
服务器端:
using System.Web.Services;
[WebMethod()]
//[ScriptMethod()]
public static void SendMessage(string subject, string message, string messageId, string pupilId)
{
//Send message
}
客户端:
$("#btnSendMessage").live("click", function(){
var subject = $("#tbSubject").val();
var message = $("#tbMessage").val();
var messageId = $("#hdnMessageId").val();
var pupilId = $("#hdnPupilId").val();
$.ajax({
type: "POST",
url: "./MessagePopup.aspx/SendMessage",
data: ("subject=" + subject + "&message=" + message + "&messageId=" + messageId + "&pupilId=" + pupilId),
error: function(XMLHttpRequest, textStatus, errorThrown){
alert(textStatus);
},
success: function(result){
alert("success");
}
});
return false;
});
我已经添加在服务器端的SendMessage方法一个破发点,但它从来没有击中它,但是当我运行code jQuery的成功方法被调用。可能是什么原因?`
I've added a break point on the server side SendMessage method, but it's never hitting it, but when I run the code the jQuery success method is called. What could be causing this?`
推荐答案
要调用ASP.NET AJAXScriptServices和第方法,需要使用完整的$。阿贾克斯()语法:
To call ASP.NET AJAX "ScriptServices" and page methods, you need to use the full $.ajax() syntax:
$.ajax({
type: "POST",
url: "MessagePopup.aspx/SendMessage",
data: "{subject:'" + subject + "',message:'" + message + ",messageId:'" + messageId + "',pupilId:'" + pupilId +"'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Do something interesting here.
}
});
请参阅此职位上,为什么这是必要的细节:<一href=\"http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/\">http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/
See this post for details on why that's necessary: http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/
编辑:扩展名不会更改为.ASMX但仍的.aspx
The extension doesn't change to .asmx but remains .aspx.
这篇关于通过调用jQuery的ASP.NET服务器端方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!