shopify php 应用程序 api url 在 javascript 中不起作用

问题描述 投票:0回答:0

我正在用 PHP + ReactJs 开发一个应用程序,我创建了一个脚本标签,为此我在项目之外创建了一个 JS 文件,并将它调用到我的控制器文件中。

<?php

public function StCreate(Request $request)
{      
         
    $shopDomain = "xyz.myshopify.com";
    $accessToken = "shpat_0dfe2de8c1c791sdsdsda9a8e2c7a2de9a5901";
    
    // Set the script tag properties
    $scriptTag = [
        'event' => 'onload',
        'src'   => 'https://www.abcd.in/upload/custom.js'
    ];
    
    // Create the script tag using the Shopify API        
    $response = Http::withHeaders([
        'X-Shopify-Access-Token' => $accessToken,
        'Content-Type'           => 'application/json'
    ])->post("https://{$shopDomain}/admin/api/2023-04/script_tags.json", [
        'script_tag'            => $scriptTag,
        'display_scope'         => 'cart'

    ]);
    
    return $response->json();    
}

现在我想在 custom.js 之外添加一个 API custom.js 代码

fetch('/api/get-saved-form-data')
  .then(response => response.json())
  .then(formData => {
    
    console.log('Hello World...');    

  })
  .catch(error => {
    console.error('Error fetching form data:', error);
});

但是在 Shopify 中,我使用 Cloudflare 隧道运行应用程序,并且每次运行应用程序时都会生成新的隧道,使用 命令“npm run dev” 现在的问题是,我尝试使用上面的 custom.js 代码在项目外部的 js 中运行我的 API。 在这个获取函数“fetch('/api/get-saved-form-data')”中这个 API 创建 URL

like..https://xyz.myshopify.com/api/get-saved-form-data(这是Shopify的实际网址) 但实际上,URL 像 https://tunnel-url/api/get-saved-form-data

那不是被创造出来的。那么如何在此处添加该隧道 URL?

请帮我解决这个问题

谢谢。

javascript reactjs shopify-app script-tag
© www.soinside.com 2019 - 2024. All rights reserved.