gpt4 book ai didi

Javascript:将变量传递给 ShopifyApp 初始化

转载 作者:行者123 更新时间:2023-11-30 17:33:51 24 4
gpt4 key购买 nike

我正在构建一个使用 Shopify Embedded App SDK 的应用程序.我试图从请求 url 中提取一个参数,并将其传递给嵌入式应用程序的 javascript 初始化函数。然而,捕获的变量没有按预期插入。

我可以成功地从请求中捕获参数,但是变量永远不会在初始化函数中输出。

到目前为止,这是我的代码:

<script src="//cdn.shopify.com/s/assets/external/app.js?123445566"></script>

<script type="text/javascript">
//Capture the params from the URL = Works fine
var urlParams;
(window.onpopstate = function () {
var match,
pl = /\+/g, // Regex for replacing addition symbol with a space
search = /([^&=]+)=?([^&]*)/g,
decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
query = window.location.search.substring(1);

urlParams = {};
while (match = search.exec(query))
urlParams[decode(match[1])] = decode(match[2]);
})();

//This is the part I am having troubles with, the urlParams["shop"] just outputs "urlParams["shop"]" instead of the value
ShopifyApp.init({
apiKey: "thiscontainsthecorrectapikeyvalue", // Expects: 32 character API key string like ff9b1d04414785029e066f8fd0465d00
shopOrigin: 'https://' + urlParams["shop"], // Expects: https://exampleshop.myshopify.com
debug: true
});

//This alert works as expected, and shows the correct value: "exampleshop.myshopify.com"
alert(urlParams["shop"]);
</script>

我可以使用普通的 javascript 或 jquery,但请注意我对这两者都不熟悉,主要是熟悉 rails(我不能在这个特定页面中使用它)。

关于如何将捕获的参数传递给初始化函数有什么想法吗?

最佳答案

不确定可能是异步概念的问题,弄好后使用urlParams

(window.onpopstate = function () {
......
......
urlParams = {};
while (match = search.exec(query)){
urlParams[decode(match[1])] = decode(match[2]);
}

callInit(urlParams); //use when it is made
})();

function callInit(urlParams){
ShopifyApp.init({
apiKey: "thiscontainsthecorrectapikeyvalue", //API Key
shopOrigin: 'https://' + urlParams["shop"],
debug: true
});

}

关于Javascript:将变量传递给 ShopifyApp 初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22436463/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com