我正在创建一个模块,该模块通过GET请求获取SKU并返回一个具有该产品属性的JSON对象。
我的indexController功能代码如下所示
public function pinboardgetitemsAction(){
$sku = $_GET[sku];
$product = Mage::getModel('catalog/product')
->loadByAttribute('sku', $sku);
$fullItem = array();
$fullItem[0] = $product->getName();
$fullItem[1] = $product->getFormatedPrice();
$fullItem[2] = $product->getSmallImageUrl();
echo json_encode($fullItem);
}
当我在浏览器中加载此请求时,它会正确返回JSON对象
http://www.jinkou.info/pinboard/index/pinboardgetitems?sku=50065
但是,当我执行AJAX请求时(我正在使用jQuery),$ sku未设置并且抛出了致命错误。
查看此行为的最简单方法是使用http://hurl.it
答案 0 :(得分:1)
除了我的拼写错误评论之外,访问params的适当方法是通过请求对象:
$sku = $this->getRequest()->getParam('sku');
答案 1 :(得分:0)
benmarks写了正确答案。
对不起,但是下一个代码:
echo json_encode($fullItem);
不合适。您应该在标题中设置json内容类型,并在响应正文中设置json主体:
/** @var $helper Mage_Core_Helper_Data */
$helper = Mage::helper('core');
$this->getResponse()->setHeader('Content-type', 'application/json')
$this->getResponse()->setBody($helper->jsonEncode($fullItem);