简体   繁体   English

我想念什么? AJAX JavaScript JSON数组解析为php

[英]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 摘要

  1. Get source of loaded images and put into a JSON array 获取已加载图像的来源并放入JSON数组中
  2. Pass JSON array to php file 将JSON数组传递到php文件
  3. Perform mysql query to get more images 执行mysql查询以获取更多图像
  4. pHp puts images into a new array of images pHp将图像放入新的图像数组中
  5. Pass new images array to javascript 将新的图像数组传递给javascript
  6. Javascript output images. JavaScript输出图像。

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 文件目录

  • ..\\Gallery\\index.php .. \\ Gallery \\ index.php
  • ..\\Gallery\\getimages.php .. \\ Gallery \\ getimages.php
  • ..\\Gallery\\js\\ajaxStuff.js .. \\ Gallery \\ js \\ ajaxStuff.js

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.

相关问题 对php文件的Ajax javascript调用没有任何作用。 我想念什么? - Ajax javascript call to a php file is not doing anything. What am I missing? 将PHP函数连接到AJAX请求:我缺少什么? - Hooking up a PHP function to an AJAX request: What am I missing? 我在使用Javascript的PHP中遇到数组到JSON结构的问题 - I am having issues with Array to JSON structure in PHP to be used in Javascript 使用来自PHP的AJAX解析JSON数组 - Parse JSON array with AJAX from PHP 我无法使用JSON从Ajax解码数组,我在做什么错 - I am unable to decode an array from ajax using JSON, what am I doing wrong 使用AJAX从php文件传递JSON字符串后,如何将JSON字符串解析为Javascript中的多维数组? - How to parse my JSON string into a multidimensional array in Javascript after passing JSON string from php file with AJAX? 用Java脚本突出显示单词,我想念的是什么? - Highlighting words with Javascript, what am I missing? 我在这个javascript贷款计算器上错过了什么? - What am I missing on this javascript loan calculator? 如果还有Else和Javascript,我会缺少什么? - If Else and Javascript what am I missing? Javascript正则表达式中的“ AND”…我缺少什么? - “AND” in Javascript regex… what am I missing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM