从jquery getJSON循环访问JSON

时间:2013-12-09 20:13:21

标签: php jquery json

以下是代码:

        $.getJSON( base_url + '/ajax/sortListings.php', { sort: sort }, function( data ) {
            $.each(data, function(i, json) {

                $( '#listings' ).append($('<div>').load( base_url + '/partialviews/listingAdminPrev.php', {
                  id: json.id,
                  name: json.name,
                  logo: encodeURIComponent(json.logo),
                  address: json.address,
                  city: json.city,
                  state: json.state,
                  zip: json.zip,
                  phone: json.phone,
                  email: json.email,
                  web_link: encodeURIComponent(json.web_link),
                  services: json.services,
                  category: json.category,
                  status: json.status,
                  created: json.created
                } ));

            });
        });

当我手动转到sortListings.php文件并将$ _POST变量转换为$ _GET时,它可以正常工作。所以该文件没有任何问题。但无论如何它在这里:

include_once('../../app/scripts/config.php');
$listingObject = Listing::getInstance();

$results = $listingObject->get_listings($_POST['sort']);

echo json_encode($results);

该文件返回:

[
    {
        "id": "32",
        "user_id": "32",
        "logo": "32_52a0960ba791c.jpg",
        "name": "Anthony Thomas Advertising",
        "address": "380 S. Main St.",
        "city": "Akron",
        "state": "AL",
        "zip": "44311",
        "phone": "3302536888",
        "email": "wayne@anthonythomas.com",
        "web_link": "http://www.aol.com",
        "status": "1",
        "services": "dfhfdhdfh",
        "category": "1",
        "created": "2013-12-05 09:32:56"
    },
    {
        "id": "20",
        "user_id": "10",
        "logo": "10_529f96001390d.png",
        "name": "Graphic Installation Services",
        "address": "2808 Broadway Blvd Unit 1",
        "city": "Monroeville",
        "state": "PA",
        "zip": "15146",
        "phone": "3306599898",
        "email": "graphic@graphicinstallationservices.com",
        "web_link": "",
        "status": "4",
        "services": "Graphic installation services",
        "category": "1",
        "created": "2013-12-04 11:35:11"
    },
    {
        "id": "21",
        "user_id": "10",
        "logo": "10_529f9c1a8375d.png",
        "name": "Intellect Productions",
        "address": "2915 13th St NW",
        "city": "Canton",
        "state": "OH",
        "zip": "44708",
        "phone": "3309334833",
        "email": "mouseywings@live.com",
        "web_link": "",
        "status": "1",
        "services": "Car Wrap Installations by Intellect Productions",
        "category": "1",
        "created": "2013-12-04 15:20:15"
    },
    {
        "id": "19",
        "user_id": "10",
        "logo": "10_529cf170b08d7.png",
        "name": "International Installations Inc",
        "address": "833 Wooster Rd N",
        "city": "Barberton",
        "state": "OH",
        "zip": "44203",
        "phone": "3306586526",
        "email": "internationalinstallers@internationinstallers.com",
        "web_link": "http://intellectproductions.com/",
        "status": "1",
        "services": "We install:\r\n· vehicle wraps\r\n· decals\r\n· vehicle lettering\r\n· banners\r\n· billboards\r\n· murals\r\nInternational Image Application Inc. is PDAA certified. We strive to deliver a constant flow of high quality work using best materials in the business, and unsurpassed skill. This ensures that our clients receive value for money, and more bang for their buck!\r\nAnd to ensure that our clients continue to receive the highest quality of work possible, we stay on top of technological trends, new materials, and installation techniques. This dedication to continued education has resulted in many clients coming back again and again.\r\nFrom a simple vehicle wraps installation to an entire ad campaign or fleet, International Image Application Inc. is your destination for precision graphics installation on virtually any medium.",
        "category": "1",
        "created": "2013-12-04 10:32:52"
    }
]

这是5个列表...所以它正在提取数据。但是,除了$ .each()之外什么也没有用。我甚至尝试过警告并没有任何东西。

不会抛出控制台错误..

1 个答案:

答案 0 :(得分:0)

你遇到了问题。当您调用执行GET的sort时,您期望$_POST中的getJSON()参数。

决定是否要使用GET或POST并保持一致。

我的猜测是你还需要在Listing::get_listings()方法中加入更好的错误处理,以便在你没有得到预期传递给它的数据时提供有用的错误。