使用Polymer的core-ajax如何向Golang服务器发送POST请求?

huangapple go评论86阅读模式
英文:

Post request to golang server using polymer core-ajax?

问题

我正在尝试使用Polymer的core-ajax进行POST请求,向运行Golang的服务器发送请求。经过了很多搜索(因为我对这方面的知识还不熟悉),我最终得到了以下代码。另外,GET请求是正常工作的。我不明白如何使用core-ajax传递POST参数。

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

我做错了什么?任何反馈都将非常有帮助!谢谢 ^.^

编辑:如果有更多信息的话,这是一些截图:使用Polymer的core-ajax如何向Golang服务器发送POST请求?

英文:

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.

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

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

curl -i -H &#39;Content-Type: application/json&#39; -d &#39;{&quot;Name&quot;:&quot;Batman&quot;,    
     &quot;Email&quot;:&quot;batman@gmail.com&quot;}&#39; 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

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

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

api.Use(&amp;rest.CorsMiddleware{
    RejectNonCorsRequests: false,
    OriginValidator: func(origin string, request *rest.Request) bool {
        return origin == &quot;http://0.0.0.0:8000&quot;
    },
    AllowedMethods: []string{&quot;GET&quot;, &quot;POST&quot;, &quot;PUT&quot;},
    AllowedHeaders: []string{
        &quot;Accept&quot;, &quot;Content-Type&quot;, &quot;X-Custom-Header&quot;, &quot;Origin&quot;},
    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.. 使用Polymer的core-ajax如何向Golang服务器发送POST请求?

答案1

得分: 2

我认为CORS是一个误导。问题可能是你发送的数据是表单编码而不是JSON。我在一个用户的类似问题中找到了一个错误。

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?参考这个答案

根据Polymer的文档

> body: 当method === "POST"时,可选的原始请求体内容。
>
> 示例:

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

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

{
  &quot;Error&quot;: &quot;invalid character &#39;\\&#39;&#39; looking for beginning of value&quot;
}

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:

&lt;core-ajax method=&quot;POST&quot; auto url=&quot;http://somesite.com&quot;
    body=&#39;{&quot;foo&quot;:1, &quot;bar&quot;:2}&#39;&gt;
&lt;/core-ajax&gt;

huangapple
  • 本文由 发表于 2015年4月29日 14:41:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/29936106.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定