2013-12-09 39 views
0

下面是代碼:遍歷JSON從jQuery的的getJSON

 $.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": "[email protected]", 
     "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": "[email protected]", 
     "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": "[email protected].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": "[email protected]", 
     "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()之外,沒有什麼可以工作的。我甚至嘗試過提醒事情,但沒有任何事情。

沒有控制檯錯誤拋出..

+0

用調試程序遍歷代碼。 –

回答

0

你遇到了你的問題。您期待$_POST中的sort參數,而您正在調用執行GET的getJSON()

決定是否使用GET或POST並保持一致。

我的猜測是你還需要在Listing::get_listings()方法中提供更好的錯誤處理方法,以便在您沒有獲得預期傳遞給它的數據時提供有用的錯誤。

+0

如果使用json調用返回false,您將如何獲取?這是拋棄我的部分。純PHP很容易拋出錯誤和抓取。我只是不太懂得如何使用JSON,因爲我從來沒有使用過這個。如果該函數返回false,則它不會執行任何操作。 – Peanut

+0

而get_listings()有一個默認參數'name',所以..並不真正需要調試D: – Peanut

+0

@Pananut你應該做的是檢查數據庫結果。如果沒有要發送的請求結果,只需將某種指標返回給調用的AJAX。這可能類似於「{」error「:」某個錯誤消息「}'。或者,你可以返回一個非200(即500)的HTTP響應代碼,它將觸發允許調用錯誤處理回調函數而不是成功回調。 –