400(错误请求)- 在 angular2 中将内容类型更改为 application/graphql
400 (Bad Request) - Change the content type to application/graphql in angular2
我试过很多次把内容类型改为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)
请求Headers
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
我已经解决了这个问题。这是服务器端问题。服务器端应包含 CORS 配置。
我试过很多次把内容类型改为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)
请求Headers
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
我已经解决了这个问题。这是服务器端问题。服务器端应包含 CORS 配置。