10 Replies Latest reply on Dec 18, 2007 1:43 PM by pmuir

    External AJAX access to webremote?

    whafrog

      Hi, is there an easy way to access @WebRemote methods from Ajax on pages that are not part of the hosted web app? In our case, we have many static web pages we host for other companies. Some of them would like to get dynamic data (via Ajax) from our web app. I'd love to use a Seam service and the Seam Ajax libraries, but the problem is visibility of the Seam remoting javascript resources, as a page not deployed with the app has no visibility of:

      <script type="text/javascript" src="/seam/resource/remoting/resource/remote.js"></script>
      <script type="text/javascript" src="/seam/resource/remoting/interface.js?myWebRemoteService"></script>
      


      even if I prefix "/seam..." with "http://localhost:8080/..."

      Is there a way to get around this, or am I stuck making a basic Servlet on the web app, and using the XMLHttpRequest() object in javascript?

      Thanks,

      Jon


        • 1. Re: External AJAX access to webremote?
          whafrog

          Since it appears I can't get a functioning EntityManager in my servlet (I can get an entity manager, but can't inject a UserTransaction or get an EntityTransaction from the entity manager)...any suggestions? I basically want to expose a method on a stateless bean, accept a couple of parameters, and return straight XML to the awaiting javascript...

          Anyone? Please?

          :-)

          • 2. Re: External AJAX access to webremote?
            whafrog

            For those interested:

            All the examples I was looking at used @Resource to inject the UserTransaction into the servlet, but it was always null. This works better:

            ut = (UserTransaction)context.lookup("java:comp/UserTransaction");


            I wish there was a better way though.


            • 3. Re: External AJAX access to webremote?
              shane.bryzak

              The remoting stubs should work, just make sure you specify the absolute url to them.

              • 4. Re: External AJAX access to webremote?
                shane.bryzak

                Oh, and you also need to override the Seam.Remoting.resourcePath after you import the script. It defaults to "/appname/seam/resource/remoting", so you'll need to give it an absolute url also:

                Seam.Remoting.resourcePath = "http://localhost:8080/appname/seam/resource/remoting"


                Make sure this line comes after the import!

                • 5. Re: External AJAX access to webremote?
                  dakna

                  I'm also interested in this topic, and with a quick search I came up with this thread:

                  http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4069622#4069622

                  Maybe in this case it is also a cross domain policy problem, because of the different http port (80) of your static pages in comparison to your app server port (8080). Besides that, what about your static customer pages? I guess they don't have the same domain, so this is also not possible due to cross domain policy.

                  btw, I would love to see Seam remoting supporting this like EXT JS does:

                  http://extjs.com/deploy/ext/docs/output/Ext.data.ScriptTagProxy.html

                  No hands on experience on that, let me know if I'm wrong.

                  • 6. Re: External AJAX access to webremote?
                    whafrog

                     

                    "shane.bryzak@jboss.com" wrote:
                    Oh, and you also need to override the Seam.Remoting.resourcePath after you import the script. It defaults to "/appname/seam/resource/remoting", so you'll need to give it an absolute url also...


                    According to the Firefox javascript console, "Seam is not defined". Here's my whole page, which is just a file on my drive (not deployed). It's pointing to a deployed app:

                    <html>
                    <head>
                    <script type="text/javascript" src="http://localhost:8080/myapp/seam/resource/remoting/resource/remote.js"></script>
                    <script type="text/javascript" src="http://localhost:8080/myapp/seam/resource/remoting/interface.js?myservice"></script>
                    <script type="text/javascript">
                     function getStuff() {
                     alert(Seam.Remoting.resourcePath);
                     Seam.Remoting.resourcePath = "http://localhost:8080/myapp/seam/resource/remoting";
                     alert(Seam.Remoting.resourcePath);
                     Seam.Component.getInstance("myservice").getStuff("param1", "param2", getStuffCallback)
                     alert("called");
                     }
                     function getStuffCallback(result) {
                     alert(result);
                     }
                    </script>
                    </head>
                    <body>
                    Seam Remote Test:<br/>
                    <hr/>
                    <button onclick="javascript:getStuff()">Get Stuff</button>
                    </body>
                    </html>
                    


                    I trust my web.xml file is set up properly:

                    <servlet>
                     <servlet-name>Seam Resource Servlet</servlet-name>
                     <servlet-class>org.jboss.seam.servlet.SeamResourceServlet</servlet-class>
                     </servlet>
                    
                     <servlet-mapping>
                     <servlet-name>Seam Resource Servlet</servlet-name>
                     <url-pattern>/seam/resource/*</url-pattern>
                     </servlet-mapping>


                    Anything in there that might get in the way? Security?

                    Thanks to dakna for the other thread, crossing domains may be an issue in future, but I'd like to prove it can work within-domain :-)


                    • 7. Re: External AJAX access to webremote?
                      shane.bryzak
                      • 8. Re: External AJAX access to webremote?
                        whafrog

                        Now that I included jboss-seam-remoting.jar...yes. (doh! slap!)

                        Different issue now, both IE and Firefox have security issues. I'm logged in to my work via VPN, it must think "localhost" is a different domain. In IE I can enable the session, in Firefox I get a red "Please wait..." box in the upper right of the page, and an error on the console:

                        Error: uncaught exception: Permission denied to call method XMLHttpRequest.open


                        I can get past the "Please wait..." by adding the following code to my javascript method:

                        if (window.XMLHttpRequest) { // Mozilla, Safari,...
                         try {
                         netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
                         } catch (e) {
                         alert("Permission UniversalBrowserRead denied.");
                         return null;
                         }
                         }
                        


                        However, then I get this in the javascript console:

                        Error: [Exception... "'Permission denied to get property XMLDocument.documentElement' when calling method: [nsIOnReadyStateChangeHandler::handleEvent]" nsresult: "0x8057001e (NS_ERROR_XPC_JS_THREW_STRING)" location: "<unknown>" data: no]


                        Any way around that?

                        I've rewritten this service as a servlet. I still end up with the same domain-change security issues, but don't have a problem reading the data from my home-rolled javascript. So I'm wondering if there's a bug in the Seam javascript?


                        • 9. Re: External AJAX access to webremote?
                          whafrog

                          For those interested, Seam appears to be using the XmlHttpRequest.responseXML property, but this triggers a Firefox/javascript/security problem, in that you aren't allowed to access the XML version of the data you're requesting.

                          You can, however, get the raw text, do your own reparsing, and play as you please:

                          // hack, since Firefox won't allow access to responseXML
                          var xmlDoc = (new DOMParser()).parseFromString(xmlHttpRequest.responseText, "text/xml");


                          Perhaps the Seam remoting libraries should incorporate this...


                          • 10. Re: External AJAX access to webremote?
                            pmuir

                            File a feature request in jira