Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AJAX回调不';不行。_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript AJAX回调不';不行。

Javascript AJAX回调不';不行。,javascript,jquery,ajax,Javascript,Jquery,Ajax,我试着做一个简单的回调,这样我就可以习惯它了,但是当页面加载时它就不起作用了 $(document).ready(getVideoId(function (response) { alert(response); })); function getVideoId(callback){ var http; var url = base_url + "main/firstVideo"; if (window.XMLHttpRequest){ // code for

我试着做一个简单的回调,这样我就可以习惯它了,但是当页面加载时它就不起作用了

$(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);
};
}

我的想法是,我将从数据库中取回视频id并发出警报。Base_url被定义为一个变量。

您只从它自己的回调打开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);
http.onreadystatechange=function(){
如果(http.readyState==4&&http.status==200){
回调(http.responseText);
}

}; // 查看浏览器的开发工具。重新加载页面。JavaScript控制台说什么?您能在Net选项卡中看到您的Ajax请求吗?格式正确吗?它得到回应了吗?响应是否正确?为什么使用jQuery(
$(document).ready
),但仍然使用本机JS ajax代码?使用jQuery ajax API。如果您使用jQuery,为什么要手动执行ajax操作?为什么要使用长度为零的正文发出POST请求以获取数据?是否在任何地方声明了
base\u url