【问题标题】:Making calls to the shopify storefront api in jquery在 jquery 中调用 shopify 店面 api
【发布时间】:2018-01-07 01:01:45
【问题描述】:

我正在使用 shopify storefront api 进行测试,并希望使用 ajax 调用来访问它。此 api 使用 graphql,因此我大致遵循 https://www.graph.cool/docs/tutorials/graphql-and-jquery-kohj2aengo/ 的说明

$.ajax({
    type:"POST",
    url: 'https://my-test-shopify-store.myshopify.com/api/graphql',
    dataType: 'json',
    headers: {"X-Shopify-Storefront-Access-Token": "my_storefront_access_token"},
    data: JSON.stringify({"query":"{shop{name}}"}),
    contentType: 'application/json',
    success: function(data){
        console.log("successful post");
    },
    error: function(data){
        console.log(data);
        console.log('errors');
    }
});

我希望这会返回一个 json 字符串“我的测试 Shopify 商店”,但我什么也没得到。有什么明显的我做错了吗?

【问题讨论】:

  • 您是否在成功回调中尝试过console.log(data); 而不是错误回调?检查浏览器控制台是否有任何错误。
  • 是的,我试过把它放进去,或者两者都放进去。 'errors' 以任何一种方式被记录,当它在成功函数中时仅此而已,并且当我在错误函数中返回 Object {readyState: 0, responseText: "", status: 0, statusText: "error"} 时.
  • 将此行放入您的成功回调中:console.log(data.data.shop.name);

标签: jquery ajax shopify graphql


【解决方案1】:

您需要在 url 中包含 API 版本,并在末尾包含 .json。最后,您没有在success 回调中记录data。见下面修改后的代码:

$.ajax({
    type:"POST",
    url: 'https://my-test-shopify-store.myshopify.com/api/2021-01/graphql,json',
    dataType: 'json',
    headers: {"X-Shopify-Storefront-Access-Token": "my_storefront_access_token"},
    data: JSON.stringify({"query":"{shop{name}}"}),
    contentType: 'application/json',
    success: function(data){
        console.log("successful post");
        console.log(data);
    },
    error: function(data){
        console.log(data);
        console.log('errors');
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-16
    • 2022-11-02
    • 1970-01-01
    • 2017-10-14
    • 2019-09-25
    • 1970-01-01
    • 1970-01-01
    • 2022-07-04
    相关资源
    最近更新 更多