2011-02-14 113 views
2

嘿,我有一個腳本創建並響應一個JSON編碼的magento產品陣列。PHP:通過jQuery獲取JSON ajax幫助

我有一個調用使用jQuery的Ajax功能,這個腳本的腳本,但我沒有得到恰當的反應。當進行GET請求螢火蟲顯示

GET http://localhost.com/magento/modules/products/get.php 200 OK then a **red cross** then 361ms 

這是創建數組的腳本:

// Load product collection 
$collection = Mage::getModel('catalog/product')->getCollection(); 
$collection->addAttributeToSelect('name'); 
$collection->addAttributeToSelect('price'); 

$products = array(); 

foreach ($collection as $product){ 
    $products[] = array("price" => $product->getPrice(), 
         "name" => $product->getName()); 
} 

header('Content-Type: application/x-json; charset=utf-8'); 
echo(json_encode($products)); 

這裏是我的jQuery:

<script type="text/javascript"> 
     $(document).ready(function() { 
      $.ajax({ 
       type: "GET", 
       url: "http://localhost.com/magento/modules/products/get.php", 
        success: function(products) 
        { 
         $.each(products,function() 
         { 
          var opt = $('<option />'); 
          opt.val(this.name); 
          opt.text(this.price); 
          $('#products').append(opt); 
         }); 
        } 
      }); 
     }) 
</script> 

我碰到一個響應這但我沒有看到任何JSON。我正在使用螢火蟲。我可以看到出現了一個JSON編碼的響應,但響應選項卡emtyp和我的選擇框沒有選擇。

任何人都可以看到和我的代碼問題?

這是我應該得到(當我通過瀏覽器手動運行該腳本做得到)迴應:

[{"price":"82.9230","name":"Dummy"},{"price":"177.0098","name":"Dummy 2"},{"price":"76.0208","name":"Dummy 3"},{"price":"470.6054","name":"Dummy 4"},{"price":"357.0083","name":"Dummy Product 5"}] 

感謝,

比利

+0

當您剛剛打開http://localhost.com/magento/modules/products/get.php作爲頁面< – SergeS 2011-02-14 14:31:29

+0

時,您收到了什麼,我的scipt正確返回了JSON。我運行該腳本並獲得JSON編碼的產品數組 – iamjonesy 2011-02-14 14:32:03

+0

您的JSON可能有錯誤。嘗試複製get.php輸出並將其粘貼在`var a =`in firebug後 – naugtur 2011-02-14 14:38:00

回答

0

使用cache: false,作爲一個你的AJAX參數...

我知道當我在AJAX使用JSON,我不得不這樣做:

    success: function(data) { 
         $(".custName, .projectDesc").empty(); 
         for(var x in data) { 
         $(".custName").append(data[x].message1); 
         $(".projectDesc").append(data[x].message2); 
        } 

我不知道這是否會幫助你或不:)

0

您傳回PHP不是對象的數組,這就是你對待它在你的JavaScript的方式。

你的PHP更改爲:

$products = array(); 
foreach($collection as $product) { 
    $products[] = array("price" => $product->getPrice(), 
         "name" => $product->getName()); 
} 

這應返回JSON看起來像:

[{"price":"82.9230","name":"Dummy"},{"price":"177.0098","name":"Dummy 2"}, etc ] 

然後jQuery.each應該能夠遍歷對象返回數組過:

success: function(products) 
{ 
    jQuery.each(products,function() 
     { 

     var opt = jQuery('<option />'); 
      opt.val(this.name); 
      opt.text(this.price); 
      jQuery('#products').append(opt); 
     }); 
    } 
0
$.each(products,function(index, arr) 
{ 
     var opt = $('<option />'); 
     opt.val(arr[name]); 
     opt.text(arr[price]); 
     $('#products').append(opt); 
}); 

我好PE它可以幫助你

0

嘗試在$就配置對象添加數據類型屬性:

<script type="text/javascript"> 
    $(document).ready(function() { 
     $.ajax({ 
      type: "GET", 
      url: "http://localhost.com/magento/modules/products/get.php", 
      dataType : 'json', 
      success: function(products) { 
       $.each(products,function() { 
        var opt = $('<option />'); 
        opt.val(this.name); 
        opt.text(this.price); 
        $('#products').append(opt); 
       }); 
      } 
     }); 
    }) 
</script> 

也許,$就功能不知道響應數據類型...

0

添加dataType: 'json' in jQuery.ajax參數