【发布时间】:2013-05-30 23:10:24
【问题描述】:
无论如何,要从我的 OpenCart 商店、使用 Ajax、JavaScript/jQuery 的 phonegap 移动应用程序中获取 JSON 格式的产品目录。 OpenCart 允许这样的事情吗?
欢迎任何想法或代码:)
【问题讨论】:
标签: jquery ajax json jquery-mobile opencart
无论如何,要从我的 OpenCart 商店、使用 Ajax、JavaScript/jQuery 的 phonegap 移动应用程序中获取 JSON 格式的产品目录。 OpenCart 允许这样的事情吗?
欢迎任何想法或代码:)
【问题讨论】:
标签: jquery ajax json jquery-mobile opencart
OcJoy 进展顺利,但提供的解决方案将包含我猜不是您想要的输出的所有页面数据。
你应该在$this->response->setOutput($this->render()); 之前做这样的事情(假设你只需要产品 JSON 数组),而不是他的解决方案:
if(isset($this->request->get['json'])) {
echo json_encode($this->data['products']);
die;
} else
那么在点击http://www.example.com/index.php?route=product/category&path=20&json之后,只会输出ID为20的category的products数组的JSON。
编辑:要从模板中将其作为 AJAX 请求调用,您可以这样做:
$(document).ready(function(){
$.ajax({
url: 'index.php?route=product/category&path=<CATEGORY_ID>&json',
type: 'get',
dataType: 'json',
beforeSend: function() {
},
complete: function() {
},
success: function(data) {
if (data.length > 0) {
// do something here with the products in data array
}
}
});
});
确保将<CATEGORY_ID> 替换为上述URL 中的正确值,并在success 回调函数中添加所需的功能。
【讨论】:
catalog/view/theme/default/template/account/register.tpl 或 .../template/product/product.tpl - 应该有几个 jQuery.load() 或 jQuery.post() 调用...如果您在寻找合适的示例时遇到问题,请告诉我。
Access-Control-Allow-Origin 我实际上正在使用一个名为 opencart-webap 的模块,它允许您以 RESTapi 的形式获取数据。
http://domain1.com/ 加载到http://otherdomain.com/ 的另一个安装中吗?我想这超出了这个问题的范围,应该作为关于模块和完整问题描述的新问题提出......嗯?
在catalog/controller/product/catalog.php之前
$this->response->setOutput($this->render());
粘贴此代码
if (isset( $this->request->get['json'])) $this->response->setOutput(json_encode($this->data)); else
然后转到链接http://opencart.examle/index.php?route=product/category&path=20&json
【讨论】:
$this->response->setOutput($this->render()); 替换为此if(isset( $this->request->get['json'])) $this->response->setOutput(json_encode($this->data)); else $this->response->setOutput($this->render());