0
votes

I'm trying to pass data from Flash to Javascript via ExternalInterface. It works fine when I'm testing on my localhost, but when I tried to set a website at IIS, so my coworkers could test my application, like: http://192.168.0.10/MyApp, I getting this error:

Error calling method on NPObject.

After reading some questions at Stackoverflow I tried to set allowScriptAccess="always" at my embed tags and Security.allowDomain(*) at my AS file, but it still doesn't work.

I'm using swf object if that matters.

What am I missing?

EDIT: The error occurs when I try to call a method from js to my swf. This is the code adapted to the answer bellow.

var swfReady = false;
swfobject.embedSWF("swf/1.swf", "flashContent", "300", "250", "11", "expressInstall.swf", null, { allowScriptAccess: "always" } , null, function (e) {
    if (e.success) {
         setTimeout(function () {
              swfReady = true;
         }, 150);
    }
});

btnConfig.on('click', function () {

    if (swfReady) {
         flashContent.myMethod();
    } else {
         alert("Hold on...");
    }

});

This works great at localhost but when someone try to view the same page through my IP, I get the Javascript error Error calling method on NPObject.

1
Check that you have done Security.allowDomain("*"); before your ExternalInterface call - Pranav Hosangadi
I'm setting it at the constructor of my as3 class. Is that right? - hasser
Is the SWF file on the same domain as the HTML page where it's embedded? - laurent
Yes, it is! My page is at localhost and the swf at localhost/swf - hasser

1 Answers

1
votes

Sometimes this happens when the swf is not ready. You can try the callback function in embedSWF. Don't invoke any swf method until this callback. In addition to this sometimes you will need a few milliseconds of delay.

swfobject.embedSWF("sample.swf", "swfdiv", "400", "300", "9.0.0", "expressInstall.swf", null, null, null, function (e) {
    if (e.success) {
        console.log("swf ready ");
        setTimeout(function(){
            // call swf method here
        }, 150);
    } else {
        console.log("embedding failed");
    }
});