简体   繁体   English

从ajax函数Jquery调用Code Behind方法不起作用

[英]Calling of Code Behind method from ajax function Jquery not working

I have the following JQuery in my aspx page to call the Code behind function 我在aspx页面中有以下JQuery来调用后台代码

onDelete: function (item) {
      //Some Code
       $.ajax({
           type: "POST",
           url: 'Page_Name.aspx/callmethod',
           data: "{}",
           contentType: "application/json; charset=utf-8",
           dataType: "json"
       });
    }

I am calling the below Method 我正在调用以下方法

[WebMethod]
public static void callmethod()
{
    Page_Name call = new Page_Name();
    call.Function();
    call.Function_Structure();
    call.Function_items_Structure();
    call.OClear();
    call.PClear();
    call.IClear();
}

I had tried in many ways but its not working kindly any one point me what could be wrong here 我已经尝试了很多方法,但是任何地方都无法正常工作,这可能是错误的地方

What you are trying to accomplish is inherently impossible. 您试图实现的目标本质上是不可能的。 In your webmethod, I see the following lines: 在您的网络方法中,我看到以下几行:

call.pnlCH.Visible = false;
call.pnlPdtl.Visible = false;

You're trying to change the attributes of your ASP.Net form elements, but without using a postback operation. 您试图更改ASP.Net表单元素的属性,但不使用回发操作。 If you do not have a postback, ASP.Net will not re-render your page for you! 如果没有回发,ASP.Net将不会为您重新呈现页面! It will only return what you tell the webmethod to return, which in your case is void . 它只会返回您告诉Web方法返回的内容,在您的情况下为void

There are two ways to fix this, I'm not sure which it is you want: 有两种方法可以解决此问题,我不确定您要使用的是哪种:

Option 1 - No ASP.Net interference needed 选项1-不需要ASP.Net干扰

If you only want to change some element's visibility, you could do that via jQuery, you don't need your backend for that. 如果您只想更改某个元素的可见性,则可以通过jQuery来实现,因此不需要后端。

$("#myElement").show();
$("#myElement").hide();

For ASP.Net controls, you'll want to know the clientId. 对于ASP.Net控件,您需要了解clientId。

var clientId = <%= pnlCH.ClientID %>;

$("#" + clientID).hide();

Option 2 - ASP.Net is needed 选项2-需要ASP.Net

If you need your backend code for this (eg to look something up from the database, or any other good reason), doing it purely via jQuery isn't going to help. 如果您为此需要后端代码(例如,从数据库中查找内容,或任何其他正当理由),那么仅通过jQuery进行操作将无济于事。 In this case, what you'll want to be doing is perform a regular postback scenario . 在这种情况下,您要执行的是执行常规的回发方案

Having the page post back means that ASP.Net will render your page again. 将页面发回意味着ASP.Net将再次呈现您的页面。 Any change you make to the form elements will then be rendered. 您对表单元素所做的任何更改都将被呈现。

It's important to note here that a postback is required to have ASP.Net re-render your page. 重要的是,在这里需要回发才能使ASP.Net重新呈现您的页面。

So all in all, I'm not sure which way you want/need to go with this. 所以总的来说,我不确定您要/需要哪种方式。 But your current approach, while syntactically valid, will simply not work because of how ASP.Net and client-side asynchronous calls work. 但是,由于ASP.Net和客户端异步调用是如何工作的,因此您当前的方法虽然在语法上有效,但根本无法使用。

Try using {} for your data, "{}" will be considered as string or don't include data parameter if you are not sending any parameters. 尝试对数据使用{} ,如果不发送任何参数, "{}"将被视为字符串或不包含数据参数。

onDelete: function (item) {
      //Some Code
       $.ajax({
           type: "POST",
           url: 'Page_Name.aspx/callmethod',
          // data: "{}",
              data: {}, 
           contentType: "application/json; charset=utf-8",
           dataType: "json"
       });
    }

Here 这里

Try adding an error function to your $ajax call, and see what errors come back. 尝试将错误函数添加到$ ajax调用中,然后查看返回了哪些错误。

data: {},
dataType: "json",
error: function(jqXHR, textStatus, errorThrown ) {
   // debug here
   alert(jqXHR);
},
success: function() ... 

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

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