通过 jQuery 调用 ASP.NET 服务器端方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/886903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling an ASP.NET server side method via jQuery
提问by Fermin
I'm trying to call a server side method from client side via jQuery. My code is as follows:
我正在尝试通过 jQuery 从客户端调用服务器端方法。我的代码如下:
Server side:
服务器端:
using System.Web.Services;
[WebMethod()]
//[ScriptMethod()]
public static void SendMessage(string subject, string message, string messageId, string pupilId)
{
//Send message
}
Client side:
客户端:
$("#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;
});
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?`
我在服务器端 SendMessage 方法上添加了一个断点,但它从未命中它,但是当我运行代码时,jQuery 成功方法被调用。什么可能导致这种情况?`
回答by Dave Ward
To call ASP.NET AJAX "ScriptServices" and page methods, you need to use the full $.ajax() syntax:
要调用 ASP.NET AJAX“ScriptServices”和页面方法,您需要使用完整的 $.ajax() 语法:
$.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.
}
});
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/
有关原因的详细信息,请参阅此帖子:http: //encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/
Edit: The extension doesn't change to .asmx but remains .aspx.
编辑:扩展名不会更改为 .asmx,但仍为 .aspx。
回答by Stephen Newman
It looks like you're trying to make use of a page method.
看起来您正在尝试使用 page 方法。
Take a look here Page Methods in ASP.NET Ajaxfor help
在此处查看ASP.NET Ajax 中的页面方法以获取帮助
回答by Frank
$.ajax({
type: "POST",
url: "MessagePopup.aspx/SendMessage",
data: "{subject:'" + subject + "',message:'" + message + ",messageId:'" + messageId + "',pupilId:'" + pupilId +"'}",
async: true,
cache: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function() {},
error:function (xhr, ajaxOptions, thrownError){ alert(thrownError); }
});
If this doesn't work...and gives "Syntax Error: syntax error"...then add this
如果这不起作用......并给出“语法错误:语法错误”......然后添加这个
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory,
System.Web.Extensions, Version=3.5.0.0, Culture=neutral,
PublicKeyToken=31BF3856AD364E35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory,
System.Web.Extensions, Version=3.5.0.0, Culture=neutral,
PublicKeyToken=31BF3856AD364E35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler,
System.Web.Extensions, Version=3.5.0.0, Culture=neutral,
PublicKeyToken=31BF3856AD364E35" validate="false"/>
</httpHandlers>
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions,
Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</httpModules>
between </compilation>
and </system.web>
in your Web.Config file.
在您的 Web.Config 文件之间</compilation>
和</system.web>
中。
Hope this will help somebody because took me a while to figure that beside jquery function i have to add that in Web.Config.
希望这会对某人有所帮助,因为我花了一段时间才弄清楚除了 jquery 函数之外,我还必须在 Web.Config 中添加它。
回答by Artem Koshelev
You should use web service instead of regular aspx web page. Web pages has no support to call web methods, I believe your jQuery request loads the HTML page instead. I suggest you two things:
您应该使用 Web 服务而不是常规的 aspx 网页。网页不支持调用网页方法,我相信您的 jQuery 请求会加载 HTML 页面。我建议你做两件事:
- Use Fiddler2 (with IE) or HttpFox (with Firefox) to debug AJAX requests and responses on client side.
- Use WCF web service on the server side. in this case you can use SvcConfigEditorand SvcTraceViewerto configure and debug web methods on the server side.
- 使用 Fiddler2(使用 IE)或 HttpFox(使用 Firefox)在客户端调试 AJAX 请求和响应。
- 在服务器端使用 WCF Web 服务。在这种情况下,您可以使用SvcConfigEditor和SvcTraceViewer在服务器端配置和调试 Web 方法。
回答by Junimation
Here is code that might work in your situation.
这是可能适用于您的情况的代码。
<script type="text/javascript">
$(document).ready(function () {
// Add the page method call as an onclick handler for the button.
$("#btnSubmit").click(function () {
//get the string from the textbox
$.ajax({
type: "POST",
url: "testSearch.aspx/GetMyShippingDate",
contentType: "application/json; charset=utf-8",
data: "{'tracking_num': '" + $("#txtTrackingNumber").val() + "'}",
dataType: "json",
success: function (date) {
// Replace the div's content with the page method's return.
Success(date);
},
error: Failed
});
});
});
function Success(result) {
$("#ParcelShippingDate").html(result.d);
}
function Failed(result) {
alert(result.status + " " + result.statusText);
}
There is an example that has always work for me.
有一个例子一直对我有用。
Here is the complete article http://www.webdeveloperpost.com/Articles/How-to-use-jquery-ajax-in-asp-dot-net-web-page.aspx
这是完整的文章http://www.webdeveloperpost.com/Articles/How-to-use-jquery-ajax-in-asp-dot-net-web-page.aspx
It works well for those that want a straight forward way of using the jquery asp.net method call back
它适用于那些想要直接使用 jquery asp.net 方法回调的人