我试图进行简单的回调,所以我可以习惯它,但是当页面加载时它不起作用。
$(document).ready(getVideoId(function (response) {
alert(response);
}));
function getVideoId(callback){
var http;
var url = base_url + "main/firstVideo";
if (window.XMLHttpRequest){
// code for IE7+, Firefox, Chrome, Opera, Safari
http=new XMLHttpRequest();
}
else if (window.ActiveXObject){
// code for IE6, IE5
http=new ActiveXObject("Microsoft.XMLHTTP");
}
else{
alert("Your browser does not support XMLHTTP!");
}
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", 0);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
callback(http.responseText);
}
http.open("POST", url, true);
http.send(null);
};
}
想法是我会从数据库中取回video_id并提醒它。 Base_url定义为变量。
答案 0 :(得分:4)
你只是从它自己的回调中打开AJAX请求,它永远不会被激活。
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
callback(http.responseText);
}
}; // << close the function here
http.open("POST", url, true);
http.send(null);