Post 使用 polymer core-ajax 请求 golang 服务器?

Post request to golang server using polymer core-ajax?

我正在尝试使用 polymer core-ajax 向服务器 runnung golang 发出 POST 请求。经过大量搜索(因为我是新手),我得到了以下代码。此外,GET 请求工作完美。 POST 参数我不明白如何使用 core-ajax 传递。

<polymer-element name="register-user" attributes="url">
    <template>
        <core-ajax id="ajaxSubmit" url="{{url}}" contentType="application/json" handleAs="json" method="post" on-core-response="{{response}}"></core-ajax>
        <style type="text/css">
        </style>
    </template>
    <script>
    Polymer({
        buttonListener: function() {
            var data = '{"Name":"'+ this.name +'", "Email":"'+ this.email +'"}';
            this.$.ajaxSubmit.data = data;
            this.$.ajaxSubmit.go();
            console.log(data);
        },
        response: function(oldValue){
            console.log(this.response);
        }
    });
    </script>
</polymer-element>

以上代码 returns 500 (Internal Server Error) 但是当我使用 curl 发出 POST 请求时,即

curl -i -H 'Content-Type: application/json' -d '{"Name":"Batman",    
     "Email":"batman@gmail.com"}' http://so.me.ip.ad:8080/register

它正常工作并且 returns

HTTP/1.1 200 OK
Content-Type: application/json
X-Powered-By: go-json-rest
Date: Wed, 29 Apr 2015 05:40:15 GMT
Content-Length: 117

{
  "id": 3,
  "name": "Batman",
  "email": "batman@gmail.com",
  "createdAt": "2015-04-29T05:40:15.073491143Z"
}

此外,我在服务器上设置了一个 CORS 中间件,即

api.Use(&rest.CorsMiddleware{
    RejectNonCorsRequests: false,
    OriginValidator: func(origin string, request *rest.Request) bool {
        return origin == "http://0.0.0.0:8000"
    },
    AllowedMethods: []string{"GET", "POST", "PUT"},
    AllowedHeaders: []string{
        "Accept", "Content-Type", "X-Custom-Header", "Origin"},
    AccessControlAllowCredentials: true,
    AccessControlMaxAge:           3600,
})

我做错了什么?任何反馈都会有很大帮助!谢谢^.^

编辑:这里有更多信息,如果有帮助的话。

我认为 CORS 是一个转移注意力的问题。问题可能是您发送的数据是格式编码的,而不是 json。我发现了来自 similar problem.

用户的错误
HTTP/1.1 500 Internal Server Error
Content-Type: application/json
X-Powered-By: go-json-rest
Date: Fri, 12 Dec 2014 04:29:59 GMT
Content-Length: 71

{
  "Error": "invalid character '\'' looking for beginning of value"
}

也许您应该使用 .body 而不是 .data?参见 this answer

来自聚合物documentation:

body: Optional raw body content to send when method === "POST".

Example:

<core-ajax method="POST" auto url="http://somesite.com"
    body='{"foo":1, "bar":2}'>
</core-ajax>