Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.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
如何在c#中返回ajax响应?_C#_Jquery_Ajax - Fatal编程技术网

如何在c#中返回ajax响应?

如何在c#中返回ajax响应?,c#,jquery,ajax,C#,Jquery,Ajax,我是AJAX和C#的新手,所以我正在尝试如何将它们结合起来。我正在构建一个web应用程序,并在ajax函数中从c#调用一个方法,如果一切正常,那么我希望将成功消息返回给我的c#类。我该怎么做? 这是我的ajax方法 <script type="text/javascript"> Hello(); function Hello() { $.ajax({ type: "POST", url: "Dtata.as

我是AJAX和C#的新手,所以我正在尝试如何将它们结合起来。我正在构建一个web应用程序,并在ajax函数中从c#调用一个方法,如果一切正常,那么我希望将成功消息返回给我的c#类。我该怎么做? 这是我的ajax方法

<script type="text/javascript">


 Hello();
    function Hello() {
        $.ajax({
            type: "POST",
            url: "Dtata.aspx/Hello",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (result) {
                response(result.d);
            },
            error: function (result) {
                alert('There is a problem processing your request');
            }
        });
    }


</script>

  //Basically I want to know the success/failure value from the Ajax call and print it back to my console.

   protected void Page_Load(object sender, EventArgs e)
    {


    }

    [WebMethod]
    public static string Hello(string name)
    {
        return name;
    }

你好;
函数Hello(){
$.ajax({
类型:“POST”,
url:“Dtata.aspx/Hello”,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(结果){
答复(结果d);
},
错误:函数(结果){
警报(“处理您的请求时出现问题”);
}
});
}
//基本上,我想知道Ajax调用的成功/失败值,并将其打印回控制台。
受保护的无效页面加载(对象发送方、事件参数e)
{
}
[网络方法]
公共静态字符串Hello(字符串名称)
{
返回名称;
}

您是否也尝试在ajax调用中传递数据?(字符串名称)。。因为您的后端服务(webMethod)在其方法参数中有它…类似于:

 <script type="text/javascript">




        Hello();
            function Hello() {
                $.ajax({
                    type: "POST",
                    url: "Dtata.aspx/Hello",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data:{name:'hello'},
                    success: function (result) {
                        response(result.d);
                        Counter() //<-- CALL OTHER AJAX METHOD TO INCREASE COUNTER ON BACK END
                    },
                    error: function (result) {
                        alert('There is a problem processing your request');
                    }
                });
            }


 function Counter() {
                $.ajax({
                    type: "POST",
                    url: "Dtata.aspx/Counter",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",

                    success: function (result) {
                        console.log(result.d);
                    },
                    error: function (result) {
                        alert('There is a problem processing your request');
                    }
                });
            }


        </script>

          //Basically I want to know the success/failure value from the Ajax call and print it back to my console.
         private int _counter;
           protected void Page_Load(object sender, EventArgs e)
            {


            }

            [WebMethod]
            public static string Hello(string name)
            {
                return name;
            }



             [WebMethod]
                public static int Counter()
                {
                    this._counter = this._counter +1;
                    return this._counter
                }

你好;
函数Hello(){
$.ajax({
类型:“POST”,
url:“Dtata.aspx/Hello”,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
数据:{name:'hello'},
成功:功能(结果){
答复(结果d);

Counter()//您在ajax调用中丢失了数据:

var counter=0;
$.ajax({
     type: "POST",
     url: "Dtata.aspx/Hello",
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     data:{name:"test"}
     success: function (result) {
           //result is response from c#. you can do your trick here
           alert(result.d);
           if(result.d=="success"){
               counter++;
           }
     },
     error: function (result) {
          alert('There is a problem processing your request');
     }
 });

我认为您没有在ajax调用中传递字符串名称..例如:$.ajax({type:'POST',url:'Dtata.aspx/Hello',contentType:'application/json;charset=utf-8',dataType:'json',data:{name:'Hello'}成功:函数(result){response(result.d);},错误:函数(结果){alert('处理您的请求时出现问题');});您正在尝试构建循环请求响应。由C#web方法发送的响应将出现在AJAX成功中。您正在尝试再次将其重新发送到服务器。:-谢谢。但是如何将AJAX调用的成功/失败响应打印回我的C#类?抱歉..不完全理解您想要做什么..打印响应nse?…或者将其发送回后端到其他WebMethod?:-因此,我的最终目标是在我的c#类中有一个计数器。每次数据从我的c#传递到ajax方法时,计数器都会增加,并且该方法返回“成功”响应。只有当响应成功时,计数器才会增加。因此,这就是为什么我必须找到一种读取ajax响应的方法,以便在我的c#类中增加计数器值。因此,您创建了另一个jquery ajax函数,该函数调用另一个webmethod(增加计数器),并在Hello()成功时调用js函数函数:-如何将计数器值发送回我的c#类,因为计数器值将作为数组索引发送,然后由数组索引执行其余工作。因此,我必须找到一种方法将计数器值发送回c#类。您可以调用另一个ajax方法将计数器值发送回c#类:-请告诉我如何执行此操作,因为在这一点我真的很迷茫。