admin管理员组文章数量:1416623
How I can get response from graphql server using pure js without libraries?
For example how I can do that using XMLHttpRequest
?
Query and serverUrl are below:
const serverUrl = '/'
const query = {
query: `{
viewer {
date
}
}`
};
How I can get response from graphql server using pure js without libraries?
For example how I can do that using XMLHttpRequest
?
Query and serverUrl are below:
const serverUrl = 'http://example./graphql/'
const query = {
query: `{
viewer {
date
}
}`
};
Share
Improve this question
asked Aug 30, 2019 at 5:52
Arseniy-IIArseniy-II
9,2555 gold badges27 silver badges51 bronze badges
1 Answer
Reset to default 8Use POST
request with 'Content-Type', 'application/json'
const yourServerUrl = 'http://example./graphql'
const yourQuery = {
query: `{
users {
firstName
}
}`
};
// below ordinary XHR request with console.log
const xhr = new XMLHttpRequest();
xhr.responseType = 'json';
xhr.open('POST', yourServerUrl);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onload = function () {
console.log('data returned:', xhr.response);
};
xhr.send(JSON.stringify(yourQuery));
source
本文标签: javascriptHow to fetch from graphql server using XMLHttpRequestStack Overflow
版权声明:本文标题:javascript - How to fetch from graphql server using XMLHttpRequest? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745256795a2650151.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论