首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >iron-ajax发送编码请求

iron-ajax发送编码请求
EN

Stack Overflow用户
提问于 2015-06-10 09:56:30
回答 2查看 2.5K关注 0票数 0

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

下面是我的代码:

代码语言:javascript
运行
复制
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"

*/
代码语言:javascript
运行
复制
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时,它确实工作得很好。

EN

回答 2

Stack Overflow用户

发布于 2015-10-08 05:32:02

正如Zikes上面所写的,你必须确保你是用"POST“方法发送的,并将所有的JSON放入代码中。

这里可能还需要一件事: JSON.stringify(),这是我的代码,它解决了这个问题+允许动态字段:

代码语言:javascript
运行
复制
        <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>

就是这样。希望这对某些人有帮助。

票数 2
EN

Stack Overflow用户

发布于 2015-06-10 10:40:52

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

代码语言:javascript
运行
复制
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,而不是querystring。将params更改为body以解决此问题。

代码语言:javascript
运行
复制
<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 body的任何更改

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30745811

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档