Javascript ajax请求不断返回最后一个请求

Javascript ajax请求不断返回最后一个请求,javascript,php,xmlhttprequest,Javascript,Php,Xmlhttprequest,我试图让ajax返回内部图库的图像。我让它返回我想要的图像,唯一的问题是,返回的图像落后一个请求 例如,如果我调用return\u img(“test”,1,5)则不会返回图像,但当我调用return\u img(“test”,6,10)时,会返回测试相册中的图像1到5 我的javascript代码 function return_img(album, start, end) { vars = "album" + album + "&start=" + start + "&

我试图让ajax返回内部图库的图像。我让它返回我想要的图像,唯一的问题是,返回的图像落后一个请求

例如,如果我调用
return\u img(“test”,1,5)
则不会返回图像,但当我调用
return\u img(“test”,6,10)
时,会返回测试相册中的图像1到5

我的javascript代码

function return_img(album, start, end) {
    vars = "album" + album + "&start=" + start + "&end=" + end;
    _("out").innerHTML = "";

    var request = new XMLHttpRequest();
    request.open("POST", "gallery_return_img.php", true);
    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    request.onreadystatechange = function () {
        if (request.readyState === 4 && request.status === 200) {
            var returned = request.responseText;
            returned = JSON.parse(returned);
            for (i = 0; i < Object.keys(returned).length; i++) {
                x = document.createElement("div");
                y = document.createElement("img");
                y.setAttribute("src", returned[i].url);
                x.appendChild(y);
                document.getElementById("out").appendChild(x);
            }
        }
    };
    request.send(vars);
}
函数返回\u img(相册、开始、结束){
vars=“album”+album+”&start=“+start+”&end=“+end;
_(“out”)。innerHTML=“”;
var request=new XMLHttpRequest();
打开(“POST”,“gallery\u return\u img.php”,true);
setRequestHeader(“内容类型”、“应用程序/x-www-form-urlencoded”);
request.onreadystatechange=函数(){
if(request.readyState==4&&request.status==200){
返回的var=request.responseText;
returned=JSON.parse(返回);
for(i=0;i
我的PHP代码

if (key_exists('album', $_POST) && key_exists('start', $_POST) && key_exists('end', $_POST)) {
    $album = $_POST['album'];
    $start = $_POST['start'];
    $end = $_POST['end'];
    $render = json_decode(file_get_contents("/gallery_images/$album/info.pics"));
    $total = count($render);
    if($end > $total){
        $end = $total;
    }
    $pictures = [];
    for($start; $start <= $end; $start++) {
        array_push($pictures, $render[$start]);
    }
}
if(key_存在('album',$_POST)&&key_存在('start',$_POST)&&key_存在('end',$_POST)){
$album=$_POST['album'];
$start=$_POST['start'];
$end=$_POST['end'];
$render=json_decode(文件获取内容(“/gallery_images/$album/info.pics”);
$total=计数($render);
如果($end>$total){
$end=$total;
}
$pictures=[];

对于($start;$start来说,一半的人在重新阅读了XMLHttpRequest()语法后偶然发现了这个问题

Asynchronous设置为true

request.open("POST", "gallery_return_img.php", true);
我将其设置为false,不再有问题

request.open("POST", "gallery_return_img.php", false);

我不太明白为什么会有不同,但它是有效的。

欢迎使用超级用户!请注意,应该在上询问编程问题。有关我们网站的更多信息,请参阅我们的。
没有返回图像。
-检查浏览器开发人员工具控制台以检查请求返回的内容-注意:您的PHP代码看起来像是never发送任何东西,因为我猜该代码没有任何输出(很难从您发布的代码中分辨出来)服务器端元素需要花费一点时间来处理,但由于延迟,它似乎正在处理上一个请求。正如@JaromandaX您的网络选项卡所显示的那样。您的javascript for循环看起来很奇怪,幸运的是,您的PHP看起来会返回一个数组,因此代码不会分成一百万块:p也是,
vars=“album”+album
看起来不正确网络选项卡确认我发送了正确的请求并返回了正确的响应。