0
有人可以教我如何将这个示例用AJAX和jQuery调用C#web方法吗?如何使用jQuery AJAX调用无效的C#web方法?
public class Default {
[WebMethod]
public static void Example()
{
//do something
} }
有人可以教我如何将这个示例用AJAX和jQuery调用C#web方法吗?如何使用jQuery AJAX调用无效的C#web方法?
public class Default {
[WebMethod]
public static void Example()
{
//do something
} }
我已经使用下面的方法没有任何问题:
的Javascript
var dataParam='{"param":"1"}'; //or whatever data you may be sending or not sending
$.ajax({
type : "POST",
url : myHomeUrl + '/SendDataVoid',
contentType : "application/json; charset=utf-8",
data : dataParam,
datatype : "json",
}).done(function (data) {
alert('success');
}).fail(function (jqXHR, textStatus, errorThrown) {
alert(jqXHR.getResponseHeader("error"));
});
C#
[WebMethod]
public static void SendDataVoid(string param)
{
try
{
//do stuff here
}
catch (Exception ex)
{
System.Web.Services.WebService wsError = new System.Web.Services.WebService();
wsError.Context.Response.StatusCode = 500;
wsError.Context.Response.AppendHeader("error", ex.Message);
}
}
,使您能够发送一个实际的错误消息回来我而不是通用的“内部服务器”错误。
那么,什么“数据”被返回? – eaglei22 2017-03-30 20:30:48
'WebMethod' [不再支持](http://johnwsaunders3.wordpress.com/2011/12/07/more-reasons-to-not-use-asmx-services-in-new-code/)由微软。您可能希望研究基于REST的服务的替代技术,[ASP.NET Web API](http://www.asp.net/web-api)。它有一个更清洁的模型,并得到微软的支持。 – mason 2014-12-08 20:28:46