C# 在Webmethod中显示alter消息 [webMthod] HttpContext.Current.Response.Write(“警报(“您的消息”)”);

C# 在Webmethod中显示alter消息 [webMthod] HttpContext.Current.Response.Write(“警报(“您的消息”)”);,c#,asp.net,webmethod,C#,Asp.net,Webmethod,如果您对web方法执行ajax j查询调用,您可以使用failure and success方法中的alert,只从web方法返回要打印的数据 [webMthod] HttpContext.Current.Response.Write("<script>alert('your messsage')</script>"); 函数ShowCurrentTime(){ $.ajax({ 类型:“POST”, url:“WebForm3.aspx/GetCurrentTi

如果您对web方法执行ajax j查询调用,您可以使用failure and success方法中的alert,只从web方法返回要打印的数据

[webMthod]

HttpContext.Current.Response.Write("<script>alert('your messsage')</script>"); 

函数ShowCurrentTime(){
$.ajax({
类型:“POST”,
url:“WebForm3.aspx/GetCurrentTime”,
数据:“{name:”+$(“#”)[0]。值+'''}',
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:一旦成功,
故障:功能(响应){
警报(response.d);
}
});
}
函数OnSuccess(响应){
警报(response.d);
}
[System.Web.Services.WebMethod]
公共静态字符串GetCurrentTime(字符串名称)
{
返回“消息”;
}

如果您正在执行其他操作,或者您正在执行与此不同的操作,请告诉我。

您的意思是,除了方法返回的数据之外,您还需要一种显示备用消息的方法吗

为此,我使用了一个类,在该类中嵌入了我想要返回的数据。这样,所有服务方法都返回一个公共类,其中包含您想要传递的消息和调用方实际需要的数据

<script type = "text/javascript">
    function ShowCurrentTime() {
        $.ajax({
            type: "POST",
            url: "WebForm3.aspx/GetCurrentTime",
            data: '{name: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function (response) {
                alert(response.d);
            }
        });
    }
    function OnSuccess(response) {
        alert(response.d);
    }
</script> 

  [System.Web.Services.WebMethod]
        public static string GetCurrentTime(string name)
        {

            return "messaage";
        }
要使用它

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class Service_Response
{
    public string Message { get; set; }
    public dynamic Data { get; set; }

    public Service_Response()
    {

    }

    public Service_Response(string msg)
    {
        this.Message = msg;
        this.Data = null;
    }

    public Service_Response(string msg, dynamic obj)
    {
        this.Message = msg;
        this.Data = obj;
    }

    public Service_Response(string msg, object obj, Type obj_type)
    {
        this.Message = msg;
        this.Data = Convert.ChangeType(obj, obj_type);
    }
}

写(“”;请注意空格,无法提交而不提交。那么,请您提供更多有关您试图容纳的内容的信息,好吗?
[WebMethod()]
public static Service_Response GetHelloWorld() {
      return new Service_Response("hello world", true);
}
//or
[WebMethod()]
public static Service_Response GetHelloWorld(int i) {
      return new Service_Response("hello world" + i);
}
//or
[WebMethod()]
public static Service_Response GetHelloWorld(string name) {
      var data = DateTime.Now;
      return new Service_Response("hello world from " + name, data);
}