在 Flex 中的 s:HttpService 上设置 JSON 内容类型

发布于 2024-10-02 17:45:03 字数 550 浏览 3 评论 0原文

我试图在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

预谋 2024-10-09 17:45:03

看来我已经整理好了。诀窍是应该在服务中添加 Accept 标头:

       var header:Object=new Object();

        **header["Accept"] = "application/json";**

        service.contentType = "application/json";
        service.headers = header;
        service.send('{}');

我希望它对某人有帮助。祝你好运。

Looks like I have sorted it out. The trick is that Accept header should be added on service:

       var header:Object=new Object();

        **header["Accept"] = "application/json";**

        service.contentType = "application/json";
        service.headers = header;
        service.send('{}');

I wish it could be helpful for somebody. Good luck.

放低过去 2024-10-09 17:45:03

谢谢,这对我很有帮助。我将标头分配简化为:


httpService.headers = { 接受:"application/json" };

Thanks, this was very helpful to me. I simplified the header assignment to:


httpService.headers = { Accept:"application/json" };

寂寞美少年 2024-10-09 17:45:03

我想我会发布一个更干净的例子。

-------- JsonHttpService.as

package services
{
    import mx.rpc.http.HTTPService;
    import mx.rpc.http.SerializationFilter;

    public class JsonHttpService extends HTTPService
    {
        private var jsonFilter:JsonSerializationFilter = new JsonSerializationFilter();

        public function JsonHttpService(rootURL:String=null, destination:String=null)
        {
            super(rootURL, destination);
            resultFormat = "json";
        }

        override public function get serializationFilter():SerializationFilter {
            return jsonFilter;
        }
    }
}

--- JsonSerializationFilter.as

package services
{
    import mx.rpc.http.AbstractOperation;
    import mx.rpc.http.SerializationFilter;

    public class JsonSerializationFilter extends SerializationFilter
    {
        public function JsonSerializationFilter() {
            SerializationFilter.registerFilterForResultFormat("json", this);
            super();
        }

        override public function deserializeResult(operation:AbstractOperation, result:Object):Object {
            return JSON.parse(result as String);
        }

        override public function getRequestContentType(operation:AbstractOperation, obj:Object, contentType:String):String {
            return "application/json";
        }

        override public function serializeBody(operation:AbstractOperation, obj:Object):Object {
            return JSON.stringify(obj);
        }
    }
}

Thought I'd post a cleaner example.

-------- JsonHttpService.as

package services
{
    import mx.rpc.http.HTTPService;
    import mx.rpc.http.SerializationFilter;

    public class JsonHttpService extends HTTPService
    {
        private var jsonFilter:JsonSerializationFilter = new JsonSerializationFilter();

        public function JsonHttpService(rootURL:String=null, destination:String=null)
        {
            super(rootURL, destination);
            resultFormat = "json";
        }

        override public function get serializationFilter():SerializationFilter {
            return jsonFilter;
        }
    }
}

--- JsonSerializationFilter.as

package services
{
    import mx.rpc.http.AbstractOperation;
    import mx.rpc.http.SerializationFilter;

    public class JsonSerializationFilter extends SerializationFilter
    {
        public function JsonSerializationFilter() {
            SerializationFilter.registerFilterForResultFormat("json", this);
            super();
        }

        override public function deserializeResult(operation:AbstractOperation, result:Object):Object {
            return JSON.parse(result as String);
        }

        override public function getRequestContentType(operation:AbstractOperation, obj:Object, contentType:String):String {
            return "application/json";
        }

        override public function serializeBody(operation:AbstractOperation, obj:Object):Object {
            return JSON.stringify(obj);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文