How to make an externalinterface call to a JavaScript file which calls a server to get informtion

StackOverflow https://stackoverflow.com/questions/11133636

Вопрос

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.

Это было полезно?

Решение

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);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top