gpt4 book ai didi

c# - 在jquery中调用代码隐藏函数

转载 作者:行者123 更新时间:2023-11-28 01:20:22 24 4
gpt4 key购买 nike

我正在尝试执行一项非常简单的任务。不确定我做错了什么。我需要将一些值传递给代码隐藏方法,在那里进行一些计算,然后返回结果。我从一个测试方法开始。网上有很多例子。但对我来说没有任何作用。

请参阅下面我的默认页面代码:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication7._Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

<script type="text/javascript">

$(document).ready(function () {
var loc = window.location.href;
$("#btnClick").click(function (event) {
$.ajax({
type: 'POST',
url: loc + "/GetMessage",
data: "{}",
contentType: "application/json; charset=utf-8"

})
.success(function (response) {
alert(response.d);

})
.error(function (response) {
alert(response.d);
});
});
});

</script>

<input id="btnClick" type="button" value="button" />


</asp:Content>

母版页已引用jquery:

<script src="Scripts/jquery-2.1.0.js"></script>
<script src="Scripts/jquery-2.1.0.min.js"></script>

隐藏代码:

    [WebMethod]
public static string GetMessage()
{
return "test";
}

我确实收到“未定义”错误。当我更改 .success 和 .error 函数时,如下所示

           .success(function () {
alert("S");

})
.error(function () {
alert("E");
});

显示“S”。我的理解是它找到方法背后的代码。但“响应”是不确定的。这是否意味着方法背后的代码没有以 json 格式返回数据?

最佳答案

我终于通过注释掉App_Start中的代码解决了这个问题。穆拉里的建议帮助我找出了实际问题。异常(exception)是“身份验证失败”。 //settings.AutoRedirectMode = RedirectMode.Permanent;

关于c# - 在jquery中调用代码隐藏函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23340227/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com