[英]What am I missing? AJAX javascript JSON array parse to php
I am trying to pass a set of already loaded images to a php file to then access a database to load more images unique to the ones already loaded. 我正在尝试将一组已加载的图像传递到php文件,然后访问数据库以加载更多已加载图像的唯一图像。 The javascript passes an array of the sources of the images, the php file performs a query to select all images limited to 12 not in the passed array. javascript传递图像来源的数组,php文件执行查询以选择限制在12个不在传递数组中的所有图像。 php then passes back the images as JSON objects so I can use JS to appropriately handle them. php然后将图像作为JSON对象传回,因此我可以使用JS适当地处理它们。
SUMMARY 摘要
The javascript isn't loading the images, nor is it returning the test array I have in the php file of numbers. javascript不会加载图像,也不会返回数字php文件中的测试数组。 Why is it not working? 为什么不起作用?
JAVASCIRPT JAVASCIRPT
var imageArray = {};
imageArray.itemList = [];
function imagesLoaded(){
var gal_img_thumbs = document.getElementsByClassName("gal-thumb-image");
for (var i = 0; i < gal_img_thumbs.length ; i++){
var img = gal_img_thumbs[i].firstChild.getAttribute("src");
var new_obj = {'src':img};
imageArray.itemList.push( new_obj);
//imageArray.itemList.[i]['src']=img;
}
}
function loadImages(){
console.log("load images");
imagesLoaded();
$.ajax({
url: "getimages.php",
dataType: "json",
data: {
imagesArray : imageArray.itemList
},
success: function(images){
for(var i = 0; i < images.length ; i++){
console.log("image" + images[i]);
}
}
});
}
PHP 的PHP
<?php
$myArray = $_REQUEST['imagesArray'];
$images = array();
for($i = 0; $i < 10; $i++){
$image = array(
"num" => $i
);
$images[] = $image;
}
echo json_encode($images);
?>
FILE DIR 文件目录
try this 尝试这个
var imageArray = {};
imageArray.itemList = [];
function imagesLoaded() {
var gal_img_thumbs = document.getElementsByClassName("gal-thumb-image");
for (var i = 0; i < gal_img_thumbs.length; i++) {
var img = gal_img_thumbs[i].firstChild.getAttribute("src");
var new_obj = {'src': img};
imageArray.itemList.push(new_obj);
//imageArray.itemList.[i]['src']=img;
}
}
function loadImages() {
console.log("load images");
imagesLoaded();
$.ajax({
url: "/test/test.php",
dataType: "json",
data: {
imagesArray: imageArray.itemList
},
success: function(images) {
for (var i = 0; i < images.length; i++) {
console.log("image" + images[i]);
}
}
});
}
$(function(){
loadImages();
});
try adding async:false
so that ajax begins after the imagesLoaded(); 尝试添加async:false
以便ajax在imagesLoaded()之后开始。 call is completed 通话完成
Edit: another thing you may try is to use beforeSend: function() {..}
编辑:您可以尝试的另一件事是使用beforeSend: function() {..}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.