【问题标题】:How to get products in JSON format from OpenCart using phonegap/jQueryMobile如何使用 phonegap/jQueryMobile 从 OpenCart 获取 JSON 格式的产品
【发布时间】:2013-05-30 23:10:24
【问题描述】:

无论如何,要从我的 OpenCart 商店、使用 Ajax、JavaScript/jQuery 的 phonegap 移动应用程序中获取 JSON 格式的产品目录。 OpenCart 允许这样的事情吗?

欢迎任何想法或代码:)

【问题讨论】:

    标签: jquery ajax json jquery-mobile opencart


    【解决方案1】:

    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
                }
            }
        });
    });
    

    确保将&lt;CATEGORY_ID&gt; 替换为上述URL 中的正确值,并在success 回调函数中添加所需的功能。

    【讨论】:

    • 嘿,非常感谢您提供的示例,如果可能的话,您是否介意给我一个示例,如何使用 .ajax 来使用它。我以前做过,但只使用 jsonp,而且我知道 jQuery 在我的 url 之后添加了一个 ?callback。如何从中获取数据?
    • @DaimanLoks 你从 AJAX 调用这个是什么意思?喜欢来自哪个模板(页面)? OpenCart 充满了 AJAX 调用,您可以从例如catalog/view/theme/default/template/account/register.tpl.../template/product/product.tpl - 应该有几个 jQuery.load()jQuery.post() 调用...如果您在寻找合适的示例时遇到问题,请告诉我。
    • 对不起,我的意思是,使用 jQuery .ajax 方法调用它。如您所见,我对这项技术不是很了解。
    • 我在尝试获取数据时收到此错误。 Access-Control-Allow-Origin 我实际上正在使用一个名为 opencart-webap 的模块,它允许您以 RESTapi 的形式获取数据。
    • @DaimanLoks 你不是在尝试将产品从http://domain1.com/ 加载到http://otherdomain.com/ 的另一个安装中吗?我想这超出了这个问题的范围,应该作为关于模块和完整问题描述的新问题提出......嗯?
    【解决方案2】:

    在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

    【讨论】:

    • 我专门在不同版本 1.5.x 上再次检查了代码。如果您将$this-&gt;response-&gt;setOutput($this-&gt;render()); 替换为此if(isset( $this-&gt;request-&gt;get['json'])) $this-&gt;response-&gt;setOutput(json_encode($this-&gt;data)); else $this-&gt;response-&gt;setOutput($this-&gt;render());
    • 您可以通过我的解决方案获得完整的 json 响应,包括面包屑、分页、语言、产品和类别信息
    • 好的,我误解了响应,我认为它会呈现为 HTML,但带有 json 编码数据。无论如何,OP 只针对产品数组而不是整个页面请求一个解决方案,其中包含无用的面包屑、类别、类别描述、排序和分页选项等......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 2022-12-21
    • 1970-01-01
    • 2021-09-08
    相关资源
    最近更新 更多