/ / AJAX呼び出しの完了後にコードが実行されない-javascript、jquery、ajax

AJAXの呼び出し完了後にコードが実行されない - javascript、jquery、ajax

最初のイベントリスナーは正常に機能します。 2番目のイベントリスナーが実行されると、FirebugのPOSTとリターンを確認できますが、 alert("test") 決して起こりません。

$(".walkthrough").click(function(){
var url = $(this).data("story");
var num = $(this).data("revert");
$("#tableauViz").replaceWith("<div id="tableauViz"></div>")
$.post("",{method: "getTicket", url: url},function(data){
switchToStory(url,data);
$("#help").html("<p>You are currently viewing the walkthrough, click here to <a href="#" data-num=""+num+"" class="exitWalkthrough">go back</a>.</p>");
});
});
$("#addViz").click(function(){
var url = $("#Viz-URL").val();
var name = $("#Viz-Name").val();
var expression = /[-a-zA-Z0-9@:%_+.~#?&//=]{2,256}.[a-z]{2,4}b(/[-a-zA-Z0-9@:%_+.~#?&//=]*)?/gi;
var regex = new RegExp(expression);
if(url.match(regex) && name != ""){
url = url.replace("http://","").replace("https://","");
console.log(url);
$.post("",{
user_key:"5",
method: "addViz",
url:url,
name:name
},function(data){
alert("test");
},"json");
} else {
alert("Please enter a valid URL and Name");
}
});

誰かがこれを妨げる何かを見ていますか?この問題をさらにデバッグするにはどうすればよいですか?

回答:

回答№1は0

ajaxリクエストを実行するとき、それは常に良いですエラーコールバックが呼び出されるとは思わない場合でも、エラーコールバックを提供することをお勧めします。エラーコールバックがないと、ネットワークに関連しない問題を診断する方法がありません。

すべてのjQueryajaxメソッド(.loadを除く)は、done、fail、およびalwaysメソッドを持つjQuerypromiseオブジェクトを返します。 failメソッドを使用して、障害をキャッチします。

$.post(url, data, successHandler).fail(function (xhr, status, reason) {
console.log(
xhr, // this is an object, it will contain the response text, headers, etc
status, // the http status of the response, 20x or 30x for success
reason // this is the reason that the response failed
);
});

あなたの場合、空の文字列が有効なjsonではないため、失敗しました。


回答№2の場合-2

.postから最初の空の引数 ""を削除する必要があります。そうしないと、URLが空の文字列で上書きされます。

$.post({
user_key:"5",
method: "addViz",
url:url,
name:name
},function(data){
alert("test");
},"json");