无法使用JQuery AJAX调用C#Webmethod [英] C# Webmethod unable to be called with JQuery AJAX

查看:130
本文介绍了无法使用JQuery AJAX调用C#Webmethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调用我创建的网络方法.我遇到的问题是ajax调用从未调用过我的web方法;这对我来说很奇怪,因为我在同一文件中有另一个具有相同返回类型和参数的web方法,可以在ajax调用的"url"定义中很好地引用该方法.

I am trying to call a webmethod I created. The problem I'm having is that the ajax call never calls my webmethod; 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.

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

My aspx document consists of the following:

文件名 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>

我的网络方法如下所示,文件名为CS.aspx.cs:

My web method looks like the following with the filename 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";
   } 
}

我在return语句上放置了一个断点,以确保在调试模式下甚至没有调用该方法.

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.

推荐答案

如果要进行jQuery ajax调用,请确保对jQuery有引用.

Make sure you have a reference to jQuery if you want to make a jQuery ajax call.

此外,您的WebMethod的参数需要与您传入的参数名称匹配.参数为appName:

Also, the parameters of your WebMethod need to match the parameter name you are passing in. The parameter is 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:

And in the WebMethod it is appName as well:

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

您的代码中不匹配.

我也会养成关闭div和form标签的习惯.

I would also make a habit of closing your div and form tags.

这篇关于无法使用JQuery AJAX调用C#Webmethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆