我正在使用react native fetch将带有graphql数据的post http请求发送到graphql server端点。启动我的graphql服务器并在我的本机应用程序中使用POST调用fetch时,我收到以下错误:
TypeError:响应原生应用中的XMLHttpRequest网络请求失败
请求代码
<template name="QuizQuestion">
<div class="questions">
<h5 class="white-text">{{question}}</h5>
<form action="#" class="quiz-ans">
<ul class="answer-list">
<li class="answer-items animated out ">
<input class="with-gap" name="group1" type="radio" id="{{this.order}}q1" />
<label for="{{this.order}}q1">{{this.q1.value}}</label>
</li>
<li class="answer-items animated out ">
<input class="with-gap" name="group1" type="radio" id="{{this.order}}q2" />
<label for="{{this.order}}q2">{{this.q2.value}}</label>
</li>
<li class="answer-items animated out ">
<input class="with-gap" name="group1" type="radio" id="{{this.order}}q3" />
<label for="{{this.order}}q3">{{this.q3.value}}</label>
</li>
<li class="answer-items animated out ">
<input class="with-gap" name="group1" type="radio" id="{{this.order}}q4" />
<label for="{{this.order}}q4">{{this.q4.value}}</label>
</li>
</ul>
<div class="test-controlls">
<button class="btn waves-effect waves-light right" type="submit" name="action">Next
<i class="material-icons right">send</i>
</button>
<span class="quiz-count right">1/10</span>
</div>
</form>
</div>
</template>
数据
qraphQLParams
let graphqlServer = config.graphQLServerUrl;
const graphQLFetcher = (graphQLParams, requestType, token) => {
let body = null; //earlier i used var body =' '; //blank value --null & blank makes a different
let headers = null;
body = graphQLParams;
headers = {
'Accept': 'application/json',
'Content-Type': 'application/graphql',
'Authorization': 'Bearer ' + token
};
return fetch(graphqlServer, {
method: requestType,
headers: headers,
body: graphQLParams,
});
};
,
有什么想法吗?
答案 0 :(得分:0)
我设法通过JSON.stringify请求的主体来实现这一点。
所以..
graphQLParams = `query login{
login(input: {userName: "${obj.input.userName}", passWord: "${obj.input.passWord}" }) {
token
}
}
`
body = JSON.stringify({
query: graphQLParams
});