简体   繁体   English

在jQuery中调用codebehind函数

[英]Call codebehind function in jquery

I am trying to do a very simple task. 我正在尝试做一个非常简单的任务。 Not sure what I am doing wrong. 不知道我在做什么错。 I need to pass some values to code behind method, do some calculations there and then return the result. 我需要将一些值传递给方法后面的代码,在那里进行一些计算,然后返回结果。 I started with a test method. 我从一种测试方法开始。 There are many examples on web. 网络上有很多示例。 But nothing working for me. 但是对我没有任何帮助。

Please see my default page code below: 请在下面查看我的默认页面代码:

<%@ 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>

The jquery has been referenced in master page: jQuery已在母版页中引用:

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

codebehind: 代码隐藏:

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

I do get "Undefined" error. 我确实收到“未定义”错误。 When I change the .success and .error function like below 当我更改.success和.error函数时,如下所示

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

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

It shows "S". 它显示“ S”。 My understanding is that it finds the code behind method. 我的理解是,它找到了方法背后的代码。 But the 'response' is undefined. 但是“响应”是不确定的。 Does it mean the code behind method is not returning the data in json format? 这是否意味着方法后面的代码未返回json格式的数据?

I have finally resolved the issue by commenting out the code in App_Start. 我终于通过注释掉App_Start中的代码解决了这个问题。 Murali's suggestion help me to find out the actual issue. 穆拉利的建议可以帮助我找出实际问题。 The exception was 'authentication failed'. 例外是“身份验证失败”。 //settings.AutoRedirectMode = RedirectMode.Permanent; //settings.AutoRedirectMode = RedirectMode.Permanent;

Try removing .d and check with console 尝试删除.d并通过控制台检查

.success(function (response) {
  console.log(response); //  in firefox/chrome
  alert(response);

})

Check the firebug console for the JSON output. 检查Firebug控制台以获取JSON输出。 Below is sample screenshot 以下是示例屏幕截图

在此处输入图片说明

Please check this one. 请检查这个。

       .success(function (data) {
            alert(data);

        })

First use only a single version( minified or unminified ) of jquery and use dataType:'json' like, 首先只使用单个版本( minified or unminified )的jquery并使用dataType:'json'例如

$("#btnClick").click(function (event) {
    $.ajax({
        type: 'POST',
        url: loc + "/GetMessage",
        dataType: 'json', // for json data, which is parsed
        success:function (response) {//combining success and error callback in ajax
            console.log(response);
        },
        error:function (response) {
            console.log(response);
        }
   });
});

You´ll need to remove the attribute runat="server" in the following line: 您需要在以下行中删除属性runat =“ server”:

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

Because that says, that the javascript code runs at the server and not at the client/browser, as excepted. 因为那样说,所以javascript代码在服务器上而不是在客户端/浏览器上运行,例外。 Changing this should fix all your problems. 更改此设置将解决所有问题。

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

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