我正在使用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字符串"My Test Shopify Store",但什么也得不到。有什么明显的我做错了什么吗?
发布于 2021-02-06 08:45:04
您需要在url和末尾的.json中包含API版本。最后,您没有在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');
}
});https://stackoverflow.com/questions/45416176
复制相似问题