0

I need to get and send request from/to server. i found a code in this site but it not working for me. api url is true and i can get data "{"out":355}" from firefox.

import com.adobe.serialization.json.JSON;

var request:URLRequest=new URLRequest();
request.url="******************************************"
request.requestHeaders=[new URLRequestHeader("Content-Type", "application/json")];
request.method=URLRequestMethod.GET;
var loader:URLLoader=new URLLoader();
loader.addEventListener(Event.COMPLETE, receive);
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, notAllowed);
loader.addEventListener(IOErrorEvent.IO_ERROR, notFound);
loader.load(request);

function receive(event:Event):void
{
    var myResults=JSON.decode(event.target.data);
    trace(myResults);
}

ERROR:

Scene 1, Layer 'MAIN PAGE', Frame 1, Line 15, Column 25 1061: Call to a possibly undefined method decode through a reference with static type Class.

A.M
  • 345
  • 4
  • 13
  • There's no such thing as `JSON.decode()` That's what the error message is telling you. Why didn't you just simply read [the Documentation](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/JSON.html) ? JSON has 2 methods `stringify`and `parse`. –  Sep 15 '17 at 08:37
  • why this work "https://stackoverflow.com/questions/10111193/get-and-parse-json-in-actionscript" but mine not work? – A.M Sep 15 '17 at 08:40
  • This code is easier than Adobe documentation codes. – A.M Sep 15 '17 at 08:41
  • I assume your code doesn't work because `JSON` is already a top level of AS3 and gets imported by default. So even if you import the old, deprecated stuff, you will still use the new stuff by default. –  Sep 15 '17 at 08:43
  • What do you mean? You just have to change one line, everything else stays the same. –  Sep 15 '17 at 08:44
  • OK, tell me which line and how i change? – A.M Sep 15 '17 at 08:45

1 Answers1

1
import com.adobe.serialization.json.JSON;

var request:URLRequest=new URLRequest();
request.url="******************************************"
request.requestHeaders=[new URLRequestHeader("Content-Type", "application/json")];
request.method=URLRequestMethod.GET;
var loader:URLLoader=new URLLoader();
loader.addEventListener(Event.COMPLETE, receive);
loader.load(request);

function receive(event:Event):void
{
    trace(event.target.data);
    var json: Object = JSON.parse(event.target.data);
    trace("json.out = ", json.out);
}

Result:

{"out":352}

json.out = 352

Ali Kazemi
  • 505
  • 7
  • 19