0

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":"[email protected]"}' 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": "[email protected]",
  "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

6
  • Browsers usually also send headers like "User-Agent" and "Referer" and those are not listed as allowed headers in your CORS middelware - perhaps thats the problem? Commented Apr 29, 2015 at 6:50
  • @ain i added "User-Agent" and "Referer" as AllowedHeaders but still get a 500 in response :( Commented Apr 29, 2015 at 7:15
  • What go packages do you use ? Try dumping (in the Go server) the complete request (e.g : all Headers and content) to inspect what is different between your curl request and your polymer request. Commented Apr 29, 2015 at 10:26
  • 1
    Did you try setting 'Access-Control-Allow-Origin' header to '*'? Commented Apr 29, 2015 at 16:28
  • Does it help setting the headers?this.$.ajax.headers='{"X-Requested-With": "XMLHttpRequest"'}; Commented Apr 30, 2015 at 23:34

1 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that worked! i had to work around CORS though (moved my polymer code on same domain)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.