PolymerJS POST JSON formatting

PolymerJS POST JSON formatting

本文关键字:formatting JSON POST PolymerJS      更新时间:2023-09-26

我正在尝试使用PolymerJS ajax-forms POST,我遇到了一个奇怪的JSON格式错误。有人能告诉我为什么钥匙周围没有引号吗?我能想到的唯一解决方法是手动构建带有键周围引号的主体。

代码片段:我如何接收值(其余部分与更改的id相同):

<div>
        <paper-input label="Title" id="course-title" floatingLabel value="{{item.title}}"></paper-input>
    </div>
 <access-core-ajax
              auto = "false"
              url="domain/courses"
              response="{{response}}"
              method="post"
              id="postCourse"
              contentType="application/json"
              headers='{"Accept": "application/json", "Content-Type":"application/json"}',
              body = "{{item}}">
            <template id="get-response-template" repeat="{{item in response.entries}}">
                    <p>Errors</p>
            </template>
        </access-core-ajax>
Polymer('create-new-course-page',{
        domReady: function() {
            console.log("Log: create-new-courses-page - Looks like we are domReady");
        },
        created: function() {
            console.log("Item initialized");
            this.item = {};
            this.data={};
        },
        createNewCourse: function(event) {
            console.log("HERE IS BODY", this.item);
            this.$.postCourse.go();

       }

JSON可以在日志中看到:

{标题:"WRU",//键的剩余&值,其中键不带"}

首先需要将正文转换为JSON字符串。JSON。Stringify可以提供帮助。

...
createNewCourse: function(e) {
    this.$.postCourse.body = JSON.stringify(this.item);
    this.$.postCourse.go();
}

您可能需要删除这里的body属性。您还可以删除auto属性,因为默认情况下它是false。