Polymer core-ajax不会发布JSON吗? [英] Polymer core-ajax won't post JSON?
问题描述
我正在使用core-ajax来检索JSON数据.将组件转换为以JSON形式发布回服务器是另一回事.在所有情况下,无论传入的contentType或handleAs参数如何,我作为输入传入的JSON对象似乎都将转换回服务器标头中的key = value.
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.
代码:
var ajax = document.querySelector('core-ajax');
ajax.method = 'POST';
ajax.handleAs = 'JSON';
ajax.contentType = 'application/json';
ajax.params = JSON.stringify(data);
ajax.go();
非常简单. 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.
我可能完全错过了一些东西.否则我们如何才能成功发布JSON数据?
I could completely be missing something. How else can we successfully POST out JSON data?
推荐答案
.params
用于URL参数.您想要将JSON作为请求正文发布吗?为此,我相信您需要设置.body
属性:
.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:
这应该可以解决问题:
ajax.body = data
请参见 https://github.com/Polymer/core-ajax/blob/master/core-ajax.html#L151
这篇关于Polymer core-ajax不会发布JSON吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!