Polymer core-ajax won't post JSON?

别来无恙 提交于 2020-01-02 05:19:27

问题


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?


回答1:


.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



来源:https://stackoverflow.com/questions/24898357/polymer-core-ajax-wont-post-json

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!