我有一個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沒有正確觸發。
非常感謝你的時間!
ShortCodeStatus()如何從Ajax請求中產生一個值?你的if語句不會等待ajax的響應。如果我沒有弄錯,他們會保持同步並繞過你的ajax。 – trevster344
謝謝你的回覆!看起來好像有一個步驟一路丟失即使阿賈克斯請求似乎正常工作 – rasantos