繁体   English   中英

iron-ajax发送编码的请求

[英]iron-ajax sends encoded request

提交表单后,我正在尝试向Mandrill API发送请求。 发送正常,但是请求包含垃圾字符。

下面是我的代码:

 Polymer({ is: 'landing-page', buttonClick: function() { this.$.landingPageForm.generateRequest(); } /* Upon click i get below request generated by iron-ajax Request URL:https://mandrillapp.com/api/1.0/messages/send.json?0=%7B&1=%22&2=k&3=e&4=y&5=%22&6=%3A&7=%22&8=B&9=h&10=-&11=f&12=6&13=p&14=n&15=X&16=Z&17=P&18=w&19=p&20=Z&21=6&22=4&23=6&24=T&25=_&26=F&27=L&28=m&29=A&30=%22&31=%2C&32=%22&33=m&34=e&35=s&36=s&37=a&38=g&39=e&40=%22&41=%3A&42=%22&43=%22&44=f&45=r&46=o&47=m&48=_&49=e&50=m&51=a&52=i&53=l&54=%22&55=%3A&56=%22&57=e&58=x&59=a&60=m&61=p&62=l&63=e&64=%40&65=d&66=o&67=m&68=a&69=i&70=n&71=.&72=c&73=o&74=m&75=%22&76=%2C&77=%0A&78=%09&79=%09&80=%09&81=%09&82=%09&83=%22&84=t&85=o&86=%22&87=%3A&88=%5B&89=%7B&90=%22&91=e&92=m&93=a&94=i&95=l&96=%22&97=%3A&98=%22&99=r&100=e&101=c&102=i&103=p&104=i&105=e&106=n&107=t&108=%40&109=d&110=o&111=m&112=a&113=i&114=n&115=.&116=c&117=o&118=m&119=%22&120=%7D&121=%5D&122=%2C&123=%0A&124=%09&125=%09&126=%09&127=%09&128=%09&129=%22&130=s&131=u&132=b&133=j&134=e&135=c&136=t&137=%22&138=%3A&139=%20&140=%22&141=S&142=u&143=b&144=j&145=e&146=c&147=t&148=%20&149=l&150=i&151=n&152=e&153=%22&154=%2C&155=%0A&156=%09&157=%09&158=%09&159=%09&160=%09&161=%22&162=t&163=e&164=x&165=t&166=%22&167=%3A&168=%20&169=%22&170=t&171=e&172=x&173=t&174=%20&175=i&176=n&177=%20&178=t&179=h&180=e&181=%20&182=m&183=e&184=s&185=s&186=a&187=g&188=e&189=%22&190=%22&191=%7D Response from server: code: -2 message: "Validation error: {"message":"Please enter an array"}" name: "ValidationError" status: "error" */ 
 THis is in my template: <form is="iron-form" id="formPost" method="post" action="/"> <paper-input name="email" id="email" label="Email Address" value="{{email}}"required></paper-input> </br> <paper-button type="submit" name="submit" raised on-click="buttonClick">Notify Me!</paper-button> </form> <iron-ajax contentType: "application/json" headers='{"Content-Type": "application/json;charset=utf-8"}' id="landingPageForm" url="https://mandrillapp.com/api/1.0/messages/send.json" method="POST" params='{"key":"2342sdfsdf","message":""from_email":"example@domain.com", "to":[{"email":"recipient@domain.com"}], "subject": "Subject line", "text": "text in the message""}' handle-as="json" > </iron-ajax> 
我正在使用以下示例使用Mandrill API https://medium.com/@mariusc23/send-an-email-using-only-javascript-b53319616782通过javascript发送电子邮件

当我使用jquery ajax时,它确实很好用。

正如Zikes所说,您必须确保使用“ POST”方法发送它,并将所有JSON放入代码中。

这里可能仍然需要一件事:JSON.stringify(),这是我的代码,可以解决问题,并允许使用动态字段:

        <iron-ajax
        url="https://mandrillapp.com/api/1.0/messages/send.json"
        headers='{"Content-Type": "application/json;charset=utf-8"}' 
        handle-as="json"
        method="POST"
        body='{{ajaxParams}}';></iron-ajax>

<script>
    Polymer({
        is: 'contact-form',
        properties: {
            name: { type:String,    notify: true    },//bind it to <iron-input> fields
            email: {    type:String,    notify: true    },
            msg: {  type:String,    notify: true    },
            ajaxParams: {
                type: String,
                computed: 'processParams(name,email,msg)'
            }
        },
        processParams: function(name,email,msg) {
                    var resp = {
                            "key": "mandrill-api-key",
                            "message": {
                                "from_email": email,
                                "from_name": name,
                                "headers": {
                                    "Reply-To": email
                                },
                                "subject": "subject",
                                "text": msg,
                                "to": [
                                    {
                                        "email": "a@b.c",
                                        "name": "email name",
                                        "type": "to"
                                    }
                                ]
                            }
                        };
                    return JSON.stringify(resp);

        }
     });
</script>

这就对了。 希望这对某人有帮助。

当我通过JSON验证程序运行该params JSON时,它指出了一些问题:

Parse error on line 3:
...",    "message": ""from_email":"example
----------------------^
Expecting '}', ':', ',', ']'

Parse error on line 3:
...ssage": "from_email": "example@domain.co
-----------------------^
Expecting '}', ',', ']'

此外,Mandrill API似乎希望body包含POST JSON,而不是查询字符串。 更改paramsbody来解决这个问题。

<iron-ajax
  headers='{"Content-Type": "application/json;charset=utf-8"}'              
  id="landingPageForm" 
  url="https://mandrillapp.com/api/1.0/messages/send.json" 
  method="POST"
  handle-as="json"
  body='{
    "key": "YOUR API KEY HERE",
    "message": {
      "from_email": "YOUR@EMAIL.HERE",
      "to": [
          {
            "email": "RECIPIENT_NO_1@EMAIL.HERE",
            "name": "RECIPIENT NAME (OPTIONAL)",
            "type": "to"
          },
          {
            "email": "RECIPIENT_NO_2@EMAIL.HERE",
            "name": "ANOTHER RECIPIENT NAME (OPTIONAL)",
            "type": "to"
          }
        ],
      "autotext": "true",
      "subject": "YOUR SUBJECT HERE!",
      "html": "YOUR EMAIL CONTENT HERE! YOU CAN USE HTML!"
    }
  }'></iron-ajax>

上面的代码可以作为替代品,只需使用http://jsonlint.com/验证对JSON主体的任何更改

暂无
暂无

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

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