在 Flex 中的 s:HttpService 上设置 JSON 内容类型
我试图在 httpservice 上设置 json 内容类型以使 REST 服务返回 json 数据。当我在 fiddler 中添加内容类型时,一切正常,因此问题出在 Flex 应用程序中,而不是 Web 服务中。但下面的代码不起作用,我得到的是 xml 数据而不是 json。
谁能给我提供解决方法/解决方案?
mxml:
<s:HTTPService id="service" method="POST" url="server.com"
result="loaded(event)" fault="fault(event)"
useProxy="false" resultFormat="text">
动作脚本:
public function loadAllSamples():void {
service.contentType = "application/json";
service.send('something');
}
I am trying to set the json content type on httpservice to make REST service return the json data. When I add the content type in fiddler all works fine so the problem lays in flex application, not in the web-service. But the code below does not work and I get the xml data instead of json.
Could anyone provide me the workaround/solution?
mxml:
<s:HTTPService id="service" method="POST" url="server.com"
result="loaded(event)" fault="fault(event)"
useProxy="false" resultFormat="text">
actionscript:
public function loadAllSamples():void {
service.contentType = "application/json";
service.send('something');
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看来我已经整理好了。诀窍是应该在服务中添加 Accept 标头:
我希望它对某人有帮助。祝你好运。
Looks like I have sorted it out. The trick is that Accept header should be added on service:
I wish it could be helpful for somebody. Good luck.
谢谢,这对我很有帮助。我将标头分配简化为:
httpService.headers = { 接受:"application/json" };
Thanks, this was very helpful to me. I simplified the header assignment to:
httpService.headers = { Accept:"application/json" };
我想我会发布一个更干净的例子。
-------- JsonHttpService.as
--- JsonSerializationFilter.as
Thought I'd post a cleaner example.
-------- JsonHttpService.as
--- JsonSerializationFilter.as