简体   繁体   English

无法使用JQuery AJAX调用C#Webmethod

[英]C# Webmethod unable to be called with JQuery AJAX

I am trying to call a webmethod I created. 我正在尝试调用我创建的网络方法。 The problem I'm having is that the ajax call never calls my webmethod; 我遇到的问题是ajax调用从未调用过我的web方法; this is strange to me because I have another webmethod located in the same file with the same return type and parameters that is able to be referenced fine in the "url" definition of the ajax call. 这对我来说很奇怪,因为我在同一文件中有另一个具有相同返回类型和参数的web方法,可以在ajax调用的“ url”定义中很好地引用该方法。

My aspx document consists of the following: 我的aspx文档包含以下内容:

File name CS.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>

My web method looks like the following with the filename CS.aspx.cs: 我的网络方法如下所示,文件名为CS.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";
   } 
}

I've put a breakpoint at the return statement, just to make sure that the method is even called, while in debug mode, and have had no luck. 我在return语句上设置了一个断点,以确保在调试模式下该方法甚至被调用,并且没有运气。

Make sure you have a reference to jQuery if you want to make a jQuery ajax call. 如果要进行jQuery ajax调用,请确保对jQuery有引用。

Also, the parameters of your WebMethod need to match the parameter name you are passing in. The parameter is appName: 另外,您的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);
    }
});

And in the WebMethod it is appName as well: 在WebMethod中,它也是appName:

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

In your code, there is a mismatch. 在您的代码中,不匹配。

I would also make a habit of closing your div and form tags. 我也会养成关闭div和form标签的习惯。

You need to place this service in a WCF or ASMX service and create a JavaScript proxy for it. 您需要将此服务放在WCF或ASMX服务中,并为其创建JavaScript代理。 See http://msdn.microsoft.com/en-us/library/bb532367(v=vs.90).aspx 参见http://msdn.microsoft.com/zh-cn/library/bb532367(v=vs.90).aspx

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

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