doz15449 2015-04-29 06:41
浏览 39
已采纳

使用聚合物核心Ajax向Golang服务器发布请求?

I am trying to make a POST request using polymer core-ajax to server runnung golang. After a lot of search (because i am new to this stuff) i ended up with the following code. Also, GET request is working perfect. POST parameters i dont understand how to pass using 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>

above code returns 500 (Internal Server Error) however when i make a POST request using curl i.e

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

it works as it should and 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"
}

also, i have a CORS middleware set up on server i.e

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,
})

What am i doing wrong? Any feedback will be of great help! Thanks ^.^

Edit : here is a little more info if it can help.. screenshot

  • 写回答

1条回答 默认 最新

  • dougongnan2167 2015-05-01 04:34
    关注

    I think CORS is a red herring. The problem may be that you are sending the data form-encoded and not as json. I found a bug from a user with a 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"
    }
    

    Perhaps you should use .body instead of .data? See this answer.

    From the polymer 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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog