在jquery中调用shopify店面api

时间:2017-07-31 12:38:38

标签: jquery ajax shopify graphql

我正在使用shopify店面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字符串" My Test Shopify Store"但是什么也没得到。我做错了什么明显的事情?

1 个答案:

答案 0 :(得分:0)

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

$.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');
    }
});