简体   繁体   English

从jquery getJSON遍历JSON

[英]Loop through JSON from jquery getJSON

Here is the code: 这是代码:

        $.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
                } ));

            });
        });

When I manually go to the sortListings.php file and turn the $_POST variables to $_GET, it works fine. 当我手动转到sortListings.php文件并将$ _POST变量设置为$ _GET时,它可以正常工作。 So nothing is wrong with the file. 因此,该文件没有任何问题。 But here it is anyway: 但是无论如何,这里是:

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

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

echo json_encode($results);

That file returns this: 该文件返回以下内容:

[
    {
        "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"
    }
]

Which is 5 listings.. so it's pulling the data fine. 这是5条清单..因此它可以使数据正常运行。 However, nothing is working beyond the $.each().. I even tried alerting stuff and nothing. 但是,除了$ .each()之外,什么都没有起作用。我什至尝试警告任何东西。

No console errors are thrown either.. 也不会引发任何控制台错误。

You hit on your problem. 您遇到了问题。 You are expecting the sort parameter in $_POST while you are calling getJSON() which executes a GET. 在调用执行GET的getJSON() ,需要在$_POST使用sort参数。

Make up your mind whether you want to use GET or POST and be consistent. 下定决心要使用GET还是POST并保持一致。

My guess is you also need to put better error handling in Listing::get_listings() method to provide useful errors when you are not getting the data your expect passed to it. 我的猜测是,您还需要在Listing::get_listings()方法中进行更好的错误处理,以在未获取期望的数据时提供有用的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM