0

I have a javascript file which has a function calling a server (url) to get information. Here is the javascript function:

sendQuery = function(devstr) {

    var baseUrl = 'http://serverurl/' + devstr;

    alert("the url is " + baseUrl);

    $.ajax(baseUrl, {
       type: 'GET',
       dataType: 'json'
    }).done(function(results){
       return results;
    }).fail(function(e) {
       showError(e);
    });
}

and from actionscript I am calling this function as follows:

var result:String = ExternalInterface.call("sendQuery", parameter) as String;

The call actually happens (I got an alert from javascript with the right url) but then result is simply empty or null. There is no result coming back. I added an alert to "done" in javascript and that alert does not get called.

I tried to use ExternalInterface.addcallback but I can't pass parameters and I am not entirely sure if that will work either.

I am not entirely sure to make the call from Actionscript to the Javascript file, which calls the server, and then get the information back to actionscript so I can parse it and do more with it.

I also should note that some of the operations I will be performing require POST operations. The reason I didn't do this through actionscript is because it requires having a crossdomain.xml file on the server I am requesting this information from.

Thanks in advance for the help.

user220755
  • 4,358
  • 16
  • 51
  • 68
  • So if the alert on done does not fire, that means there's something wrong with either your JavaScript code or the request itself. Has nothing to do with `ExternalInterface` or flash. You should troubleshoot your JS code for errors. – joncys Jun 21 '12 at 07:45
  • joncys, thank you for the comment. I did debug my javascript code and tried without actionscript and it worked fine. The result gets returned fine (and an alert works too). By the way, I tried to add an alert to the failure part too and that does not get fired either ... – user220755 Jun 21 '12 at 07:47
  • 1
    Well, getting back the request is an asynchronous process. So you can't immediately return a value to flash in the `ExternalInterface` call. You should either use some sort of serialization and data request processes, or if you want to stick to JavaScript the solution would be to call JS from AS, then to call AS from JS to inform, that the process has finished, and the to call JS from AS, to retrieve the result. `URLLoader` is actually the way to go. – joncys Jun 21 '12 at 07:58
  • How do I send JavaScript the actionscript callback function? – user220755 Jun 21 '12 at 15:33

1 Answers1

0

What you are trying to do, can be done without Javascript in pure ActionScript3.0 by URLLoader class:

 var baseUrl:String = 'http://serverurl/'+parameter;
 var request:URLRequest = new URLRequest(baseUrl);
 request.contentType = 'application/json';
 request.method = URLRequestMethod.GET;    //For 'POST' request-> URLRequestMethod.POST
 var urlLoader:URLLoader = new URLLoader();
 urlLoader.addEventListener(Event.COMPLETE, function(e:Event):void{
     var result:* = e.target.data;
 });
 urlLoader.load(request);
Engineer
  • 47,849
  • 12
  • 88
  • 91
  • doesn't that require having a crossdomain.xml file on the site I am requesting information from since it is not on the same domain. Also, does that work for sending POST requests too? – user220755 Jun 21 '12 at 15:26
  • @user220755 If the information you are requesting is not on the same domain, why are you using `Ajax` in your code (within your question)???!!!! It won't work. But in this case `Flash` will work, but of course you must have `crossdomain.xml` file. And of course with `URLLoader` you can send "POST" requests either.See my update. – Engineer Jun 21 '12 at 17:12
  • I can't force the other domain to have a crossdomain.xml file so I need to do it through javascript – user220755 Jun 21 '12 at 17:36
  • @user220755 Does another domain/site support JSONP? And thanks for downvoting. – Engineer Jun 21 '12 at 17:41
  • I am not sure but I would assume not. The main question I guess is: if there a way to call javascript from actionscript and get a response back *only after* javascript has called the server and got a response back? – user220755 Jun 21 '12 at 17:57
  • 1
    @user220755 Sending Ajax requests to another domain, is **IMPOSSIBLE**. Consider this->http://en.wikipedia.org/wiki/Same_origin_policy .This would be a *SERIOUS SECURITY VIOLATION*. `Javascript Ajax` can't do it,but `flash` can do it, only if `crossdomain.xml` file exists. So you are out of luck,for doing requests from client-side in your case (there is not `crossdomain.xml`, and `JSONP` is not supported). – Engineer Jun 21 '12 at 18:03