JavaScript AJAX PHP问题

编程入门 行业动态 更新时间:2024-10-10 19:22:03
本文介绍了JavaScript AJAX PHP问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

好的.我需要这方面的帮助.由于某种原因,onreadystatechange被触发了多次.我真的需要今晚弄清楚这个问题.这是我剩下的最后一项任务,我不知道该怎么办或导致了什么.请帮忙.

Ok. I need help with this. For some reason the onreadystatechange is fired multiple times. I really need to get this figured out tonight. It's the last task I have left and I don't know what to do or what's causing it. Please help.

我正在使用AJAX(ndhr)将JSON'Y-m-d h:i:s'发送到PHP,以使用strtotime()函数通过AJAX返回'm-d-Y'.JSON和PHP很好用,但是当onreadystatechange被触发时,它会多次执行.几乎比readyState ==快4倍.

I'm using AJAX (ndhr) to send over JSON 'Y-m-d h:i:s' to PHP to use the strtotime() function to return 'm-d-Y' back through AJAX. The JSON and PHP work great, but when the onreadystatechange is fired it does it multiple times. Almost like the readyState == 4 more times than it does.

var divs_d = ["d_2009", "d_2010", "d_2011"]; function ajax_get_json(cdiv,ocdv,ed){ var hr = new XMLHttpRequest(); hr.open("GET", "/json/sample.json", true); hr.setRequestHeader("Content-type", "application/json", true); hr.onreadystatechange = function () { if (hr.readyState == 4 && hr.status == 200) { cdiv.innerHTML = ""; var data = JSON.parse(hr.responseText); var cad = datam_archive; var rndate; var nda = new Array(); var ndac = 0; var ec = 0; for (ni = 0; ni < cad.length; ni++) { if (cad[ni].year == ocdv) { ec = ec + 1; ed.innerHTML = '<h4>' + ocdv + ' (' + ec + ' entries)</h4>'; var ndhr = new XMLHttpRequest(); var url = "/inc/strtotime.php"; var vars = "ndate=" + cad[ni].publish_date; ndhr.open("POST", url, true); ndhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); ndhr.onreadystatechange = function () { if (ndhr.readyState == 4 && ndhr.status == 200) { nda[ndac] = ndhr.responseText; ndac = ndac + 1; } } ndhr.send(vars); } } nda.sort(function (a, b) { return b - a }); for (ndai = 0; ndai < ndac; ndai++) { cdiv.innerHTML += '<h4><a href="/this_is_a_Test/archive.php?cdate=' + nda[ndai] + '">' + nda[ndai] + '</a></h4>'; } } } hr.send(null); } function optionCchange() { var ocdv = document.getElementById("optionCdate").value; var ed = document.getElementById("ediv"); for (i = 0; i < divs_d.length; i++) { var cdiv = document.getElementById(divs_d[i]); if (divs_d[i] == "d_" + ocdv) { cdiv.className = "bddiv show"; ajax_get_json(cdiv,ocdv,ed); } else { cdiv.className = "bddiv hide"; } } }

推荐答案

在您的 ndhr.onreadystatechange 函数中, ndhr 表示最后创建的 ndhr 在不是调用方的循环中,要引用调用对象,请使用 this .

In your ndhr.onreadystatechange function ndhr represents the last ndhr created in the loop not the calling one, to reference the calling object use this.

ndhr.onreadystatechange = function () { if (this.readyState == 4 && this.status == 200) { nda[ndac] = this.responseText; ndac = ndac + 1; } }

由于ajax的异步特性,最后一个for(ndai = 0; ndai< ndac; ndai ++)的行为与您预期的一样,在执行代码时,ajax请求已经具有还没完成.您必须在准备就绪的更改状态回调中执行此代码.只需使用计数器检查所有ajax请求是否已完成,然后执行代码即可.

The the last for(ndai = 0; ndai < ndac; ndai++) is behaving as you expect because of the asynchronous nature of ajax, by the time that code is executed the ajax requests have not finished yet. You'll have to execute this code in the on ready change state callback. Just use a counter to check if all the ajax requests have finished then execute the code.

更多推荐

JavaScript AJAX PHP问题

本文发布于:2023-10-10 17:48:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479307.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JavaScript   AJAX   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!