2

Ajax、JavaScript/jQuery を使用して phonegap モバイル アプリケーションから、私の OpenCart ストアから JSON 形式で製品カタログをフェッチする方法はありますか。OpenCart はそのようなことを許可していますか?

アイデアやコードは大歓迎です:)

4

2 に答える 2

2

OcJoy は順調に進んでいますが、提供されたソリューションにはすべてのページ データが含まれており、それはあなたの希望する出力ではないと思います。

彼のソリューションの代わりに、前に次のようなことを行う必要があります (製品の JSON 配列のみが必要であると仮定します) $this->response->setOutput($this->render());:

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 のカテゴリの製品配列の 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
            }
        }
    });
});

上記の URL を正しい値に置き換え<CATEGORY_ID>、コールバック関数内に必要な機能を追加してsuccessください。

于 2013-06-05T09:00:24.057 に答える
1

以前の 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に移動します

于 2013-06-04T18:05:49.497 に答える