简体   繁体   English

在 shopify 中将数据从前端发送到后端

[英]Sending data from frontend to backend in shopify

I'm developing an app with node and react.我正在开发一个带有节点的应用程序并做出反应。 In this app, I have created a scriptTag using the fetch function to get customer id and product id whenever a specific button is pressed.在这个应用程序中,我使用 fetch function 创建了一个 scriptTag,以在按下特定按钮时获取客户 ID 和产品 ID。 Now when I have fetched that data, I want to store it in my firebase database, which I cannot import in my script tag file because it's on the front end.现在,当我获取该数据时,我想将其存储在我的 firebase 数据库中,我无法将其导入我的脚本标记文件中,因为它位于前端。 Therefore, I need to send the data from my frontend to the backend of the app but I'm confused that if I use the fetch function to make POST and GET requests, what URL should I use?因此,我需要将数据从我的前端发送到应用程序的后端,但我很困惑,如果我使用 fetch function 发出 POST 和 GET 请求,我应该使用什么 URL? Moreover, can the fetch function even post the variable values or not?此外,获取 function 甚至可以发布变量值吗?

Scripttag.js Scripttag.js

const header = $('header.site-header').parent();

header.prepend('<div>Hello this is coming from the public folder </div>').css({'background-color':'orange', 'text-align': 'center'})

function addWishList(customer, productid){
/*
  fetch(`url`, {
    method: 'POST',
    headers: {
      'Content-Type': ,
      "X-Shopify-Access-Token": ,
    },
    body: 
    }})
  })

*/

    console.log('adding item to the wishlist!!')
}

function removeWishList(){
    console.log('removing item from the wishlist!!')
}
 var wishbtn = document.querySelector('.wishlist-btn')
 wishbtn.addEventListener('click', function(){
     if(this.classList.contains('active')){             //condition to check if the button is already pressed
        removeWishList();
        this.classList.remove('active');
        this.innerHTML = 'Add to wishlist'
     }
     else{
        var customer = wishbtn.dataset.customer;
        if(customer == null || customer == ''){
            console.log('Please log in to add this item to your wishlist')
        }
        else{
            var productid = wishbtn.dataset.product;
            this.classList.add('active');      //when the user presses add to wishlist button, it add active to the button's class
            this.innerHTML = 'Remove from wishlist'
            addWishList(customer, productid);
        }
     }
     
 })

The best way to send data from the frontend to the backend is to use the Proxy option that is provided in the Shopify Partner Dashboard.将数据从前端发送到后端的最佳方式是使用 Shopify 合作伙伴仪表板中提供的代理选项。

在此处输入图像描述

This way you can stay in the Shopify environment since it will pass specific arguments to the request that you can check if the request is truly coming from Shopify and you will have one less worry about securing it from outside requests.这样您就可以留在 Shopify 环境中,因为它会将特定的 arguments 传递给请求,您可以检查该请求是否真的来自 Shopify 并且您不必担心保护它免受外部请求的影响。

So for example you will make a fetch request to /apps/YOUR_CUSTOM_PATH and this will proxy to your URL https://example.com/custom_path where you will handle the request.因此,例如,您将向/apps/YOUR_CUSTOM_PATH发出 fetch 请求,这将代理到您的 URL https://example.com/custom_path您将在其中处理请求。

You can see more info here: https://shopify.dev/tutorials/display-data-on-an-online-store-with-an-application-proxy-app-extension您可以在此处查看更多信息: https://shopify.dev/tutorials/display-data-on-an-online-store-with-an-application-proxy-app-extension

You can create a public route for your app as well that can be requested from everywhere and allow for CORS but it's usually more work to do so... so pick your poison.您也可以为您的应用程序创建一条公共路线,该路线可以从任何地方请求并允许 CORS 但这样做通常需要更多的工作......所以选择你的毒药。

Have in mind that when creating a proxy page you need to reinstall the app on the store to take effect.请记住,在创建代理页面时,您需要在商店中重新安装应用程序才能生效。

PS: Don't ever use the Access Token in the frontend, that should be never present in the frontend. PS:永远不要在前端使用访问令牌,它不应该出现在前端。 The AccessToken is only back-end way of communicating with the API! AccessToken 只是与 API 通信的后端方式!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM