[英]How to call a void C# web method with jQuery AJAX?
有人可以教我如何使用AJAX和jQuery調用這個示例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);
}
}
這使您能夠發回實際錯誤消息而不是通用的“內部服務器”錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.