在asp.net中使用JQuery的问题?

时间:2016-12-30 15:20:17

标签: jquery asp.net

我试图在我的aspx页面中调用jquery函数,但它一直给我错误信息。我尝试了逐步调试,但仍然无法弄清楚任何事情。请有人告诉我可能是什么问题。谢谢。 我一直收到失败回复“'处理您的请求时出现问题'

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Dtata.aspx.cs" Inherits="Dtata" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
<script src="/scripts/jquery-3.1.1.min.js"></script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
        <script type="text/javascript">
            klm();
            function klm() {
                $.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>



    </form>


</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Dtata : System.Web.UI.Page
{
    private int _counter = 0;

    protected void Page_Load(object sender, EventArgs e)
    {


    }

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

    [WebMethod]
     public int Counter(int _counter)
     {
         Console.WriteLine("I have been called");

        this._counter = this._counter +1;
     return this._counter;
     }
}

1 个答案:

答案 0 :(得分:0)

请你改变这一行:

 data: { name: 'hello' },

到此:

 data: "{ 'name' : 'hello' }",

如果仍然出错,你可以发送吗? 如果你想知道究竟发生了什么,请改变这一行:

error: function (result) {
                    alert('There is a problem processing your request');
                }

error: function (result) {
                    alert(result.responseText);
                }

然后发送它的错误。