我多次尝试将内容类型更改为 application / json 或 application / graphql 。由于此 text / plain 内容类型,我无法获取graphql结果。请给我一个解决方案。我已经提供了我的代码和控制台结果。
app.module.ts
// Create the client as outlined above
const client = new ApolloClient({
networkInterface: createNetworkInterface({
uri: 'http://192.168.1.177:8080/graphql',
opts: {
method: 'POST',
mode: 'no-cors',
}
}),
});
export {
client
}
export function provideClient(): ApolloClient {
return client;
}
testgraphql.component.ts
export class TestgraphqlComponent implements OnInit {
loading: boolean;
author: any;
authors: ApolloQueryObservable<any>;
constructor(private apollo: Apollo) { }
ngOnInit() {
this.authors = this.apollo.watchQuery({
query: gql`{
user(id: 5) {
firstName
lastName
}
}`,
});
}
}
控制台错误
POST http://192.168.1.177:8080/graphql 400 (Bad Request)
请求标题
POST /graphql HTTP/1.1
Host: 192.168.1.177:8080
Connection: keep-alive
Content-Length: 77
accept: */*
Origin: http://localhost:4200
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
content-type: text/plain;charset=UTF-8
Referer: http://localhost:4200/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
答案 0 :(得分:0)
我已经解决了这个问题。这是服务器方面的问题。服务器端应包含CORS配置。