weixin_33724659 2013-07-15 12:35 采纳率: 0%
浏览 39

大海捞针jQuery

Basically, I'm producing a JSON Array dynamically to Javascript, I'm then doing an Ajax call which returns JSON data from an API, I want to check an value inside the objects from the Ajax JSON response to see if it matches with my JSON Array, I've tried doing the following :

My Dynamic JSON Array - JSON.php

global $wpdb;
$Ids = array();
$table_name = $wpdb->prefix . "videos";
$idList = $wpdb->get_results("SELECT `videoId` FROM `$table_name`");
foreach($idList as $ID) {
     $Ids[] = $ID->videoId;
}
echo json_encode(array('IdList' => $Ids));

My Dynamic JSON output example - JSON.php

{"IdList":["47","55"]}

My Dynamic JSON Array - Javascript

var Ids = null;
jQuery.ajax({
    type: "GET",
    url: pluginURL + "/JSON.php",
    data: "ajaxAction=getIds&rand="+Math.floor(Math.random()*10000),
    success: function(data) {
        Ids = jQuery.parseJSON(data);
    },
    error: function() {
        alert("Error");
    }
});

So that's the code that's producing an Array of ID's that are stored inside my Database, I then want to use an AJAX call which returns an JSON Response from an API to check against these IDs, Here's the code that's returning the API response

jQuery.ajax({
    type: "GET",
    url: apiURL + "video/" + Username,
    success: function(data) {
        Parsed = jQuery.parseJSON(data);
        jQuery.each(Parsed.user_media, function(i,v){
            id = v['id'];
            if(jQuery.inArray(id,Ids['IdList']) !== -1) {
               //this isn't working!
            }
        });
    },
});

Example data output from the AJAX call

{"user_media":[{"id":"2"},{"id":"44"}]}

As you can see I've tried to utilise the jQuery.inArray() to no avail, I'm not sure why, as I may just be parsing something wrong

  • 写回答

1条回答 默认 最新

  • weixin_33736649 2013-07-15 12:46
    关注

    May be Ids data is not getting set before second ajax call. Wrap second ajax call on first ajax call success like this:

    var Ids = null;
    jQuery.ajax({
        type: "GET",
        url: pluginURL + "/JSON.php",
        data: "ajaxAction=getIds&rand=" + Math.floor(Math.random() * 10000),
        success: function(data) {
            Ids = jQuery.parseJSON(data);
    
            jQuery.ajax({
                type: "GET",
                url: apiURL + "video/" + Username,
                success: function(data) {
                    Parsed = jQuery.parseJSON(data);
                    jQuery.each(Parsed.user_media, function(i, v) {
                        id = v['id'];
                        if (jQuery.inArray(id, Ids['IdList']) !== -1) {
                            //this isn't working!
                        }
                    });
                },
            });
        },
        error: function() {
            alert("Error");
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名