小编duu*_*ude的帖子

从 React 调用 Shopify API

到目前为止我所做的

我按照本指南使用 Rails 后端 + React 设置 Shopify 应用程序:https ://medium.com/@esimonian16/setting-up-a-shopify-app-with-rails-5-1-webpack-react-and -polaris-b8535d911275

一切似乎都运行良好。我正在使用 shopify_app gem。

当我在商家仪表板中导航到我的应用程序时,Rails 中的家庭控制器呈现一个 React 应用程序,该应用程序显示一些产品(这些产品是从 Rails 传递到作为 json 响应的)。


我现在想做什么

我想在 React 的“列出客户”中有一个按钮,然后应该更新反应组件并将所有客户显示为列表。

我在以下位置创建了一个新控制器 my-app.com/customers

class CustomersController < ShopifyApp::AuthenticatedController
    def index
        customers = ShopifyAPI::Customer.find(:all, params: { limit: 10 })
        render json: customers
    end
end
Run Code Online (Sandbox Code Playgroud)

如果我my-app.com/customers在浏览器中访问,我会得到正确的 json 响应。


然后我尝试从 React 获取这些数据。

fetch('https://my-app.com/customers', {
      headers : { 
        'Content-Type': 'application/json',
        'Accept': 'application/json'
       }
})
.then(response => response.json())
.then(parsedJSON => {
  console.log(parsedJSON);

  // Update state
  // this.setState(...);
}) …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails shopify shopify-app

5
推荐指数
1
解决办法
497
查看次数

标签 统计

ruby ×1

ruby-on-rails ×1

shopify ×1

shopify-app ×1