Angular2 ES5 - 覆盖HTTP POST标头中的Content-Type

时间:2016-09-28 13:56:23

标签: angular angular2-forms

尝试覆盖标题的内容类型,但它仍然以text / plain的形式出现。有一种方法可以与Ben Nadel的GateWayAPI合作,但希望有一个解决方案不涉及自定义包装。

    var url = this.url;
    var body = JSON.stringify({email_login: "login", password_login: "password"});
    var headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });


    return this.http.post(url, body, {headers:headers})
        .map(function (response) {
            return response.json();
        })
        .catch(this.handleError);

3 个答案:

答案 0 :(得分:1)

如何使用append函数:

bitmapLeft = (Bitmap) mainForm.pictureBoxLeft.Image;
bitmapRight = (Bitmap)mainForm.pictureBoxRight.Image;

Image<Gray, Byte> imageLeft = new Image<Gray, Byte>(bitmapLeft);
Image<Gray, Byte> imageRight = new Image<Gray, Byte>(bitmapRight);
Image<Gray, Byte> imageDisparity = new Image<Gray, Byte>(bitmapLeft.Width, bitmapLeft.Height);

StereoBM stereoBM = new StereoBM(16, 15);
StereoMatcherExtensions.Compute(stereoBM, imageLeft, imageRight, imageDisparity);

Image bitmapDisparity = imageDisparity.ToBitmap();

答案 1 :(得分:1)

post的请求Http具有以下签名:

post(url: string, body: any, options?: RequestOptionsArgs)

第三个参数是类型为RequestOptionsArgs的参数,这意味着您必须将RequestOptions传递给它,而不是Headers。您还应该使用箭头操作符(=>)而不是function。您正在寻找的是这样的:

result: any;

httpPostTest() {

var url = this.url;
var body = JSON.stringify({email_login: "login", password_login: "password"});

let headers: Headers = new Headers({'Content-Type': 'application/x-www-form-urlencoded' });
let options: RequestOptions = new RequestOptions({ headers: headers });

this.http.post(url, body, options)
    .map(response => {
        response = response.json();
    })
    .catch(this.handleError);
}

在此之后,你需要使用subscribe来获取response,所以让我们说这个函数被称为httpPostTest(我将它添加到上面)并且我们希望将响应存储在名为{的变量中{1}}:

result

答案 2 :(得分:0)

您应该创建RequestOptions类的实例,并在使用form-urlencoded时使用字符串而不是json对象:

 var body = "email_login=login&password_login=password";
 var headers = new Headers();
 headers.append('Content-Type', 'application/x-www-form-urlencoded');
 let options = new RequestOptions({ headers: headers });
 this.http.post(url, body, options);