2017-04-16 72 views
0

我有一個HTTP處理程序(ASHX),我從UI端使用AJAX函數調用。以下是此次調用中需要發生的事情:ASP.net/JS/JQuery/AJAX - ajax工作但處理程序事件沒有正確觸發

當該部分加載時,它將在短碼狀態範圍內顯示服務器上短碼的狀態。它會開或關說:

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

這是越來越短碼的狀態的功能,這工作正常。我可以手動更改短碼的狀態和變化正確地反映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"); 
 
        } 
 
       } 
 
      }); 
 
     };

這是處理程序的短代碼狀態代碼:

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

我希望能夠到c舔onggleshortcode div來通過處理程序觸發此事件。對於禁用和啓用短代碼功能是否工作正常:

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

這裏是Ajax調用的短代碼切換:

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

我正確地點擊了網址,並收到了正確的回覆每個功能。但是,對短代碼的更改似乎沒有發生。

例如,如果短代碼處於關閉狀態,則ShortCodeStatus函數將返回false並因此呈現OFF按鈕。當我點擊toggleshortcode按鈕時,輸出是真的(我想打開短代碼),但是當ShortCodeStatus函數在成功時再次觸發時,它仍然會顯示爲false。 Ajax函數似乎是正確的,但我無法弄清楚爲什麼處理器上的toggleshortcode沒有正確觸發。

非常感謝你的時間!

+0

ShortCodeStatus()如何從Ajax請求中產生一個值?你的if語句不會等待ajax的響應。如果我沒有弄錯,他們會保持同步並繞過你的ajax。 – trevster344

+0

謝謝你的回覆!看起來好像有一個步驟一路丟失即使阿賈克斯請求似乎正常工作 – rasantos

回答

0

我看到2個可以檢查的案例。

首先,在Ajax調用的「toggleshortcode」,你調用的函數「得到 ShortCodeStatus()」,並在您的實例庫函數的正確名稱是「ShortCodeStatus()」。其次,在'Handler.ashx?action = toggleshortcode'的調用中,您已經返回了狀態(true或false),我建議您製作一個名爲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); 
     } 
    }); 
}); 
+0

謝謝你的答覆! 'getShortCodeStatus()'和'ShortCodeStatus()'是我的一個錯字。我會給這個鏡頭 – rasantos

+0

沒有骰子:(服務器目前有短代碼和狀態呈現正常。但是,當使用您的代碼,我點擊toggleshortcode div,文本更改爲「OFF」,但顏色格式不會不適用。不幸的是,它仍然沒有正確觸及toggleshortcode。我有一種感覺,我可能會遺漏數據傳遞給處理程序中的toggleshortcode函數 – rasantos

相關問題