Post-json对象,使用iron-ajax作为application/x-www-form-urlencoded

Post json object with iron-ajax as application/x-www-form-urlencoded

本文关键字:application x-www-form-urlencoded 作为 iron-ajax 对象 使用 Post-json      更新时间:2023-09-26

在我的基于Polymer的应用程序中,我想使用与输入元素绑定的奇特JavaScript模型。

将模型POST到我的Spring控制器,但是我想作为经典的formdata执行,这样我就可以利用SpringSessionAttributes。

我这样设置iron-ajax

<iron-ajax id="saveMailing"
    method="POST"
    url="/api/mailing"
    content-type="application/x-www-form-urlencoded"
    on-response="mailingSaved">
</iron-ajax>

我有一个JavaScript方法,点击按钮即可执行请求:

saveDraft: function() {
    this.$.saveMailing.body = this.mailing;
    this.$.saveMailing.generateRequest();
}

this.mailing是一个JSON对象。这是而不是FormData()。

结果是,我在iron-request.html:421 中得到了一个JavaScript错误"Cannot read property to String of null"

有可能做我想做的事吗?我的意思是向iron-ajax传递一个JSON对象,并期望它将其转换为FormData。或者我没有正确使用iron-ajax

假设您确信this.mailing不是null,则需要首先字符串化它:

this.$.saveMailing.body = JSON.stringify(this.mailing);