For some reason I can make requests to the GraphQL API using Ajax, but not with Fetch.
Here is the working Ajax code:
query = 'query { course(id:"1069") {name}}';
variables = { "cid": course_id };
$.ajax({
url: '/api/graphql',
type: 'POST',
data: {
query: query,
variables: variables
},
success: function(result, status, xhr) {
console.log(result);
}
})
Here's my code using Fetch:
response = await fetch('/api/graphql', {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
query: query,
variables: variables
})
});
data = await response.json();
if (response.ok) {
console.log(data);
}
This generates a 422 status code and I can't figure out why. Can anyone help?