简体   繁体   English

ASP.net/JS/JQuery/AJAX-ajax可以工作,但是处理程序事件无法正确触发

[英]ASP.net/JS/JQuery/AJAX - ajax works but handler event not firing properly

I have a HTTP handler (ASHX) which I am calling from UI side using an AJAX function. 我有一个HTTP处理程序(ASHX),可以使用AJAX函数从UI端调用它。 The following is what needs to happen in this call: 以下是此调用中需要发生的情况:

When the section loads, it will display the status of the short code on the server in the shortcodestatus span. 当该部分加载时,它将在shortcodestatus跨度中显示服务器上短代码的状态。 It will either say on or off: 它会说开或关:

 <a class="btn btn-default" id="toggleshortcode">Short Code <span id="shortcodestatus"></span></a> 

This is the function for getting the status of the short code and this works properly. 这是获取短代码状态的功能,并且可以正常工作。 I can manually change the status of the short code and the changes reflect properly on the div when I reload the page: 我可以手动更改短代码的状态,并在重新加载页面时将更改正确反映在div上:

  function ShortCodeStatus() { $.ajax({ type: "GET", url: "Handler.ashx?action=shortcodestatus", success: function (output) { console.log("getShortCodeStatus: " + output); $("#shortcodestatus").empty(); if (output == "true") { $("#shortcodestatus").text("ON"); $("#shortcodestatus").addClass("btn btn-success"); } else { $("#shortcodestatus").text("OFF"); $("#shortcodestatus").addClass("btn btn-danger"); } } }); }; 

This is the short code status code from the handler: 这是处理程序的短代码状态代码:

 case "shortcodestatus": { output = ShortCodeStatus() ? "true" : "false"; } break; 

I want to be able to click on the toggleshortcode div to fire off this event through the handler. 我希望能够单击toggleshortcode div通过处理程序触发此事件。 The functions for disabling and enabling the short code are working properly: 禁用和启用短代码的功能正常运行:

 case "toggleshortcode": { if (ShortCodeStatus() == true) { DisableShortCode(); output = "false"; } else { EnableShortCode(); output = "true"; } } break; 

Here is the ajax call for the short code toggle: 这是短代码切换的ajax调用:

 $('#toggleshortcode').click(function () { $.ajax({ type: "POST", url: "Handler.ashx?action=toggleshortcode", success: function (output) { console.log("toggleshortcode: " + output); ShortCodeStatus(); } }); }); 

I'm hitting the URLs correctly and I'm getting the correct responses from each function. 我正确地找到了URL,并且每个功能都得到了正确的响应。 However the change to the short code does not seem to be happening. 但是,似乎没有发生对短代码的更改。

For example, if the short code is off, the ShortCodeStatus function will return false and thus render the OFF button. 例如,如果关闭了短代码,则ShortCodeStatus函数将返回false,从而呈现“关闭”按钮。 When I click on the toggleshortcode button, the output is true (I want to turn on short code) which is correct but when the ShortCodeStatus function fires again in the success, it will still say false. 当我单击toggleshortcode按钮时,输出为true(我想打开短代码),这是正确的,但是当ShortCodeStatus函数再次成功触发时,它仍然会显示false。 The ajax functions seem correct but I can't figure out why the toggleshortcode on the handler is not firing properly. Ajax函数似乎正确,但是我无法弄清楚为什么处理程序上的toggleshortcode无法正确触发。

Thank you so much for your time! 非常感谢您的参与!

I'm seeing 2 cases that you can check. 我看到您可以检查2种情况。

First, in the ajax call for 'toggleshortcode', you are calling the function ' get ShortCodeStatus()', and base on your example the correct name of the function is ' ShortCodeStatus() '. 首先,在对“ toggleshortcode”的ajax调用中,您正在调用函数“ get ShortCodeStatus()”,并根据您的示例,该函数的正确名称为“ ShortCodeStatus() ”。

Second, in the call to 'Handler.ashx?action=toggleshortcode', you are already returning the status (true or false), I suggest you make a javascript function named SetShortCodeStatus(status) , and use this inside of the success of both ajax request 'Handler.ashx?action=shortcodestatus' and 'Handler.ashx?action=toggleshortcode'. 其次,在对Handler.ashx?action = toggleshortcode的调用中,您已经在返回状态(是或否),我建议您制作一个名为SetShortCodeStatus(status)的javascript函数,并在两者均成功的情况下使用此函数ajax请求'Handler.ashx?action = shortcodestatus'和'Handler.ashx?action = toggleshortcode'。

function SetShortCodeStatus(status) {
    $("#shortcodestatus").empty();
    if (status == "true") {
      $("#shortcodestatus").text("ON");
      $("#shortcodestatus").addClass("btn btn-success");
    }
    else {
      $("#shortcodestatus").text("OFF");
      $("#shortcodestatus").addClass("btn btn-danger");
    }
}

function ShortCodeStatus() {
  $.ajax({
      type: "GET",
      url: "Handler.ashx?action=shortcodestatus",
      success: function (output) {
            console.log("getShortCodeStatus: " + output);
            SetShortCodeStatus(output);
      }
  });
};


$('#toggleshortcode').click(function () {
   $.ajax({
       type: "POST",
       url: "Handler.ashx?action=toggleshortcode",
        success: function (output) {
            console.log("toggleshortcode: " + output);
            SetShortCodeStatus(output);
        }
    });
});

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

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