【发布时间】: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