Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymer core-ajax won't post JSON?

Tags:

json

polymer

I'm using core-ajax to retrieve JSON data just fine. Turning the component around to post back to the server as JSON is another thing altogether. In all cases, and irrespective of the contentType or handleAs parameters passed in, it appears that my JSON object I'm passing in as an input is being converted back to key=value in the server headers.

The code:

var ajax = document.querySelector('core-ajax');

ajax.method = 'POST';
ajax.handleAs = 'JSON';
ajax.contentType = 'application/json';
ajax.params = JSON.stringify(data);

ajax.go();

Really straightforward. The logs in Go give me:

2014/07/22 14:23:09 utils.go:139: OPTIONS /1/users/173?access_token=(token)
2014/07/22 14:23:09 utils.go:124: POST /1/users/173?access_token=(token)
2014/07/22 14:23:09 users.go:379: full_name=Greg%20Johnson

We've verified that there's no transformation happening on our side. Request headers are going out just fine.

I could completely be missing something. How else can we successfully POST out JSON data?

like image 391
Greg Johnson Avatar asked Dec 11 '22 04:12

Greg Johnson


1 Answers

.params is for URL params. What you want is to post the JSON as the request body? For that, I believe you need to set the .body property:

This should do the trick:

ajax.body = data

See https://github.com/Polymer/core-ajax/blob/master/core-ajax.html#L151

like image 57
ebidel Avatar answered Dec 29 '22 16:12

ebidel