如何以"铁形式发送请求标题"?

时间:2016-12-19 07:24:22

标签: polymer polymer-1.0

我正在使用Polymer 1.7.0。如何在<iron-form>中发送请求标头?

我试过了:

form.addEventListener('iron-form-presubmit', function(event) {
    var headers = {'akash':'akash'};
    event.target.request.requestHeaders = headers;
    console.log(event.target.request.requestHeaders);
});

记录了以下文本,但我的标题未出现在请求中。

console.log(event.target.request.requestHeaders); ==&gt; "Object {content-type: "application/x-www-form-urlencoded"}"

2 个答案:

答案 0 :(得分:0)

<iron-form>.request属性为<iron-ajax>,而<iron-ajax>.requestHeaders实际上不是属于可设置属性。请改用<iron-ajax>.headers属性

form.addEventListener('iron-form-presubmit', function(event) {
  var headers = {'akash':'akash'};
  event.target.request.headers = headers;
});

codepen

答案 1 :(得分:0)

我们可以使用以下两种方式添加标题:

  1. <form is="iron-form" id="form" method="POST" action="/" on-iron-form-response="responseHandler" on-iron-form-error="errorHandler" headers = '{"akash":"akash"}'>

  2. <template> <content></content> <div class="layout vertical parent-style"> <form is="iron-form" id="form" method="POST" action="/" on-iron-form-response="responseHandler" on-iron-form-error="errorHandler"> </form> </div>

    `

    function handleLoginAction(event)
     {
        var form = Polymer.dom(event).localTarget.parentElement.parentElement;  // Get Form ref from DOM.
    
        form.headers = "{'akash':'akash'}";
    
        form.submit();
     }
    
    form.addEventListener('iron-form-presubmit', function(event) {
    console.log("Headers--------------");
    console.log(event.target.request.requestHeaders);   // {'akash':'akash'}
    });
    
    
     </script>
    </template>`