2015-11-02 25 views
2

我想知道POST調用如何在Polymer中工作。我知道我必須使用POST調用來發送敏感信息,例如用戶密碼和訪問令牌。我試着這樣做:HTTP POST如何在聚合物中工作?

<iron-ajax 
     id="AjaxPost" 
     url="/api/login" 
     method="POST" 
     content-type="application/x-www-form-urlencoded" 
     handle-as="json" 
     on-response="_handleAjaxPostResponse" 
     on-error="_handleAjaxPostError" 
     ></iron-ajax> 


this.$.AjaxPost.params = { email: "[email protected]", password: "password" }; 
this.$.AjaxPost.generateRequest(); 

但是,這將設置在URL中的參數,可以在瀏覽器控制檯等瀏覽:

POST http://localhost:8080/api/login?email=abc%40mgail.com&password=password 400 (Bad Request) 

PUT方法允許你設置的數據身體,我認爲這更安全。現在我有兩個問題:

  1. 我們可以設置POST方法的主體嗎?或者設置參數與設置主體相同?
  2. 如果可能的話,我應該怎麼提取服務器端的數據?

PS:我們沒有使用SSL的HTTPS連接。話雖如此,可以採用哪種方法來提高安全性?

回答

3

爲鐵AJAX的API文檔正文屬性定義如下:


對象默認:
體內容與所述請求,通常與「POST」請求中使用發送。
如果身體將被髮送未修改的字符串。
如果內容類型設置爲如下的值,然後身體會相應編碼。

content-type="application/json" 
    body is encoded like {"foo":"bar baz","x":1} 
content-type="application/x-www-form-urlencoded" 
    body is encoded like foo=bar+baz&x=1 

否則體將被傳遞到瀏覽器未修飾的,並且它會處理的任何編碼(例如,用於FORMDATA,BLOB ArrayBuffer)。

要發送數據的身體,你應該修改你的要求如下

<iron-ajax 
     id="AjaxPost" 
     url="/api/login" 
     method="POST" 
     content-type="application/json" 
     handle-as="json" 
     on-response="_handleAjaxPostResponse" 
     on-error="_handleAjaxPostError" 
     ></iron-ajax> 


this.$.AjaxPost.body = { "email": "[email protected]", "password": "password" }; 
this.$.AjaxPost.generateRequest();