Hey I have a script that is creating and echoing a JSON encoded array of magento products.
I have a script that calls this script using jQuery's ajax function but I'm not getting a proper response. When the GET request is performed firebug displays
GET http://localhost.com/magento/modules/products/get.php 200 OK then a **red cross** then 361ms
This is the script that creates the array:
// 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));
Here is my 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>
I'm getting a response from this but I'm not seeing a any JSON. I'm using firebug. I can see there has been a JSON encoded response but the response tab is emtyp and my select boxes have no options.
Can anyone see and problems with my code?
Here is the response I should get (and do get when I run the script manually via the browser):
[{"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"}]
Thanks,
Billy
var a=
in firebug – naugtur Feb 14 '11 at 14:38