概述
Ajax API 提供一套轻量级的 REST API endpoints,用于开发 Shoplazza 主题。
📘 注意
Ajax API 只能由 Shoplazza 托管的主题使用。
使用场景
Ajax API 的可能使用场景包括:
- 将商品添加到购物车并更新购物车商品数量。
- 展示相关商品推荐。
- 在访客输入搜索关键词时提示商品和 collections。
请参阅 Ajax API 参考文档,获取所有可用 API endpoints 的完整列表。
发起 API 请求
Ajax API 接受四种类型的 HTTP 请求:
GET请求:读取购物车和部分商品数据POST请求:为当前会话更新购物车PATCH请求:为当前会话修改购物车DELETE请求:为当前会话删除购物车
例如,要获取购物车的当前内容,请向店铺 /api/cart endpoint 发送客户端请求。
fetch(window.SHOPLAZZA.routes.root + '/api/cart')
.then(response => response.json())
.then(data => { /* do something */});
Locale-aware URLs
Stores can have dynamic URLs generated for them when they sell internationally or in multiple languages. When using the Ajax API, it’s important to use dynamic, locale-aware URLs so that you can give visitors a consistent experience for the language and country that they’ve chosen.
The global value window.SHOPLAZZA.routes.root is available to use as a base when building locale-aware URLs in JavaScript. The global value will always end in an empty string, so you can safely use simple string concatenation to build the full URLs.