简体   繁体   English

如何以“铁表格”形式发送请求标头?

[英]How to send request headers in “iron-form”?

I am using Polymer 1.7.0. 我正在使用聚合物1.7.0。 How do I send request headers in <iron-form> ? 如何以<iron-form>发送请求标头?

I tried this: 我尝试了这个:

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

which logged the following text, but my headers were not present in the request. 它记录了以下文本,但请求中没有我的标题。

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

The <iron-form>.request property is an <iron-ajax> , and <iron-ajax>.requestHeaders is actually not a settable property. <iron-form>.request属性是<iron-ajax> ,而<iron-ajax>.requestHeaders实际上不是可设置的属性。 Use the <iron-ajax>.headers property instead 使用<iron-ajax>.headers属性代替

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

codepen 码笔

We can add Headers using this two ways : 我们可以使用以下两种方式添加标题:

  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>` 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM