简体   繁体   English

ASP.Net 4.5-无法从JQuery调用Web方法

[英]ASP.Net 4.5 - Web Method Does Not Get Called From JQuery

I have set break points in the C# code and the web method is not invoked. 我在C#代码中设置了断点,并且未调用web方法。 I should see four rows returned from the database. 我应该看到从数据库返回的四行。

I do not get any errors - the only thing I see in the console is an error on the the length (in the javascript for loop ie i < data.d.length). 我没有收到任何错误-我在控制台中唯一看到的是长度错误(在JavaScript for循环中,即i <data.d.length)。

Relevant Code 相关代码

TestApp.aspx.cs TestApp.aspx.cs

    namespace WebApplication1
{
    public class User
    {
        public int?   TestID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
    }

    public partial class TestApp : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]
        public static User[] Get()
        {
            try
            {
                List<User> details = FetchData("sp_LoadData", null);
                return details.ToArray();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

TestApp.aspx TestApp.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestApp.aspx.cs" Inherits="WebApplication1.TestApp" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="http://code.jquery.com/jquery-1.12.0.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.11/js/jquery.dataTables.min.js"></script>
    <script src="TestApp.js"></script>
</head>
<body>
    <form id="form1">

                        <table id="example" class="display" cellspacing="1" width="100%" border="1">
                            <thead style="border: solid thin;">
                                <tr>
                                    <th>First name</th>
                                    <th>Last name</th>
                                    <th>Email</th>
                                    <th>Phone</th>
                                    <th>Delete</th>
                                </tr>
                            </thead>
                        </table>

TestApp.js TestApp.js

$(document).ready(function () {
    BindGrid();

function BindGrid() {
    var t = $('#example').DataTable();
    t.rows().remove().draw();

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "TestApp.aspx/Get",
        data: "{}",
        dataType: "json",

        success: function (data) {
            for (var i = 0; i < data.d.length; i++) {
                //debugger;
                t.row.add([
                            '<a class="table-edit" onclick="showRecord(' + data.d[i].TestID + ');">' + data.d[i].FirstName + '</a>',
                            data.d[i].LastName,
                            data.d[i].Email,
                            data.d[i].Phone,
                            '<button type="button" onclick="deleteRecord(' + data.d[i].TestID + ');">Delete</button>'
                ]).draw(false);
            }
        },
        error: function (result) {
            debugger;
            alert("Error");
        }
    });
};

这是控制台

网络

"I do not get any errors - the only thing I see in the console is an error on the the length", so you DO get an error :) “我没有收到任何错误-我在控制台中看到的唯一一件事就是长度错误”,因此您确实得到了一个错误:)

When you are checking the length, you are assuming there is always an array defined as 'd' in data. 在检查长度时,假设数据中始终存在一个定义为“ d”的数组。 You should add a check to validate your assumptions: 您应该添加检查以验证您的假设:

if (data && data.d && data.d.length) {
  for (var i = 0; i < data.d.length; i++) {
     ...

Use 采用

[ScriptMethod]

Below [WebMethod] 在[WebMethod]下

I suspect that since you're passing in an empty object .NET is looking for a function defined like Get(object) not Get(). 我怀疑由于您传入的是空对象,.NET正在寻找一个定义为Get(对象)而不是Get()的函数。 Try removing the below from your ajax call: 尝试从ajax调用中删除以下内容:

   data: "{}",
   dataType: "json",

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM