Set JSON content type to s: HttpService in flex

I am trying to set the json content type to httpservice so that the REST service returns json data. When I add the content type to fiddler, everything works fine, so the problem lies in the flexible application, and not in the web service. But the code below does not work, and I get xml data instead of json.

Can someone provide me a 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'); } 
+7
json flex actionscript
source share
3 answers

Looks like I figured it out. The trick is that the Accept header must be added to the service:

  var header:Object=new Object(); **header["Accept"] = "application/json";** service.contentType = "application/json"; service.headers = header; service.send('{}'); 

I would like this to help someone. Good luck.

+12
source

Thanks, it was very helpful to me. I simplified the purpose of the header:

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

+9
source

I think I would 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); } } } 
0
source

All Articles