如何使用http'POST'与form-data body而不是json body? (Angular2 /打字稿)

时间:2016-09-12 09:13:48

标签: json http angular typescript

我有一些我想要执行的请求,直到现在我使用json的http.post是这样的:

<script type="text/javascript">
    function printDiv(divName) {
        var divToPrint = document.getElementById(divName);
        var popupWin = window.open('', '_blank', 'width=1024,height=700');
        popupWin.document.open();
        popupWin.document.write('<html><link rel="stylesheet" href="css/print.css" type="text/css"><link rel="stylesheet" href="css/style.css" type="text/css"><body onload="window.print()">' + divToPrint.innerHTML +' </html>');
        popupWin.document.close();
        window.close();
    }
    function showDiv() {
        document.getElementById('tow').style.display = "block";
    }
</script>

但是这不起作用,因为这个对这个网站的请求需要是表单数据体...我怎样才能接受同样的调用并将其更改为表单数据?

谢谢!

1 个答案:

答案 0 :(得分:4)

正如我在Angular 2 tests中看到的那样,你应该使用FormData对象。即:

let body = new FormData();
body.append('email', 'someone@gmail.com');
body.append('password', '123');
this.http.post("https://somepath.com/users/login", body);