我有一个简单的API烧瓶后端,想发表一篇文章。 我不确定错误是——我收到了错误信息,但不确定是角问题还是烧瓶问题。我试过了request.get_json请求()在我的瓶子里
{BadRequest}400 BadRequest: The browser (or proxy) sent a request that this server could not understand
Angular2呼叫我的服务.ts公司名称:
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable} from 'rxjs/Rx';
import { Headers } from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
@Injectable()
export class NicksService {
private headers = new Headers({'Content-Type': 'application/json'});
nickUrl: string = "http://localhost:5000/nick";
constructor(private http: Http) { }
// Send the nick to the server to see if it is valid
checkNickValidity(nickName: string): Observable<string>{
return this.http.post(this.nickUrl, JSON.stringify({nick: nickName}), {headers: this.headers})
.map(this.extractData)
.catch(this.handleError)
}
烧瓶后端:
^{pr2}$
我想你的申请书两边都有问题。在
客户端:
Angular的Http's post方法接收body参数的任何对象,这意味着您不需要对它进行字符串化。在
例如:
服务器端:
一旦客户机的请求正确地到达服务器,您就无法正确地获取数据。在
你需要做的是:
^{pr2}$这里,json数据作为字典存储在变量
data
中。之后,从所述字典的nick
键获得该尼克。在最后以json的形式返回到客户端是更好的实践。这就是Flask's jsonify方法的作用。注意响应后附加的200和400status codes,以向客户机指示请求是否被正确接受(200)还是错误请求(400)。在
相关问题 更多 >
编程相关推荐