Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用jQueryAjax调用void C#web方法?_C#_Jquery_Ajax_Webmethod - Fatal编程技术网

如何使用jQueryAjax调用void C#web方法?

如何使用jQueryAjax调用void C#web方法?,c#,jquery,ajax,webmethod,C#,Jquery,Ajax,Webmethod,有人能教我如何用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 s

有人能教我如何用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
由Microsoft提供。您可能希望研究基于REST的服务的替代技术。它有一个更干净的模型,并且受到微软的支持。那么,“数据”返回的是什么呢?
[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);

    }
}