我正在尝试使用聚合物核心 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>
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
它可以正常工作并返回
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,
})
我究竟做错了什么?任何反馈都会有很大帮助!谢谢^.^
GCT1015
相关分类