c# Webmethod无法被JQuery AJAX调用

C# Webmethod unable to be called with JQuery AJAX

本文关键字:JQuery AJAX 调用 Webmethod      更新时间:2023-09-26

我正在尝试调用我创建的webmethod。问题是,我有ajax调用从来没有调用我的webmethod;这对我来说很奇怪,因为我有另一个web方法位于同一文件中,具有相同的返回类型和参数,可以在ajax调用的"url"定义中被引用。

我的aspx文档包含以下内容:

文件名CS。aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="CS.aspx.cs" Inherits="_Default" %>
<script src="http://code.jquery.com/jquery-1.11.1.min.js" type="text/javascript"></script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<script>
    function ShowCurrentTime() {
    if(document.getElementById("txtUserName").value == "" && document.getElementById("app_name").value == ""){
        alert("One of the fields must be filled out");
        return 0;
    }

    $.ajax({
        type: "POST",
        url: "CS.aspx/getAppId",
        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) {
    var y = document.getElementById("test");
    y.innerHTML = response.d;
    }
    </script>
    <body>
    <form id="form1" runat="server">
        <div>
    <table style="background-color:#fcfcfc; width:30%;">
    <tr>
        <td><b>Application ID:</b> </td>
        <td><asp:TextBox ID="txtUserName" runat="server"></asp:TextBox><br /></td>
    </tr>
    <tr>
        <td><b>Application Name:</b></td>
        <td><asp:TextBox ID="app_name" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td><input id="btnGetTime" type="button" value="Search" 
            onclick = "ShowCurrentTime()" /></td>
    </tr>
    </table>
    <p id="test"></p>
    </body>
    </html>

我的web方法如下所示,文件名为css .aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Web.Script.Serialization;
public partial class _Default : System.Web.UI.Page 
{
   [System.Web.Services.WebMethod]
   public static string getAppId(string appName) {
       return "this is a string";
   } 
}

我在返回语句中放置了一个断点,只是为了确保在调试模式下甚至调用了该方法,并且没有运气。

如果你想调用jQuery ajax,请确保你有一个jQuery的引用。

同样,你的WebMethod的参数需要与你传入的参数名相匹配。参数为appName:

 $.ajax({
    type: "POST",
    url: "CS.aspx/getAppId",
    data: '{appName: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: OnSuccess,
    error: function (response) {
        alert(response.d);
    }
});

在WebMethod中也是appName

[System.Web.Services.WebMethod]
public static string getAppId(string appName)
{
    return "this is a string";
}

在你的代码中,有一个不匹配。

我还会养成关闭div和form标签的习惯。

您需要将此服务放置在WCF或ASMX服务中,并为其创建JavaScript代理。见http://msdn.microsoft.com/en-us/library/bb532367 (v =应用程序). aspx