我试图弄清楚如何编写graphQL Ajax GET请求。当我在浏览器中输入complete url时,我确实得到了数据。
但是,当我想尝试通过Ajax GET方法获取数据时:
$.get({
url: 'https://ltbliqkb1c.execute-api.eu-west-1.amazonaws.com/thomas/query?query=%7B%20book(id:%229789024576807%22)%20%7B%20bol%20%7B%20id%20%7D%20itunes%20%7B%20beschikbaar%20%7D%20%7D%7D',
contentType: 'application/json',
crossDomain:true,
headers: {
'Access-Control-Allow-Origin': 'https://ltbliqkb1c.execute-api.eu-west-1.amazonaws.com/',
'Access-Control-Allow-Credentials': true
}
}).done(function(response) {
console.log('Data:', response.data);
});
我得到了:
无法加载https://ltbliqkb1c.execute-api.eu-west-1.amazonaws.com/thomas/query?query=%7B%20book(id:%229789024576807%22)%20%7B%20bol%20%7B%20id%20%7D%20itunes%20%7B%20beschikbaar%20%7D%20%7D%7D:请求标头字段Access-Control-Allow-Origin在预检响应中不允许使用Access-Control-Allow-Origin。
我的Graphql查询:
{
book(id: "9789024576791") {
bol {
id
}
itunes {
beschikbaar
}
}
}
我也试过以下js:
var test = JSON.stringify('{book(id:"9789047202363"){bol{id}}}')
$.ajax({
method: "GET",
url: "https://ltbliqkb1c.execute-api.eu-west-1.amazonaws.com/thomas/query?"+test,
contentType: "application/json",
crossDomain:true,
success: function(data) {
console.log('data',data);
},
error: function(err) {
console.log(err);
}
});
并使用以下变量进行stringify:
var test= ` book($id: String!){
bol{
id,
ean,
mobileUrl,
desktopUrl
}
itunes{
beschikbaar
auteur
titel
}
}
}`
将标题设置为access-control-allow-origin:*