4 Replies Latest reply on Feb 18, 2011 7:58 AM by erbora00

    http get request by jsfunction

    erbora00

      I need to perform a HTTP GET request by jsfunction.

      Is there a way to do it by specifying a URL or something?

      Basically, I need to do a background HTTP-GET request at page 'onload'.

       

      I am using JSF, Facelet, JBoss4.2.3, Seam 2.1.2, RichFaces 3.3.0.

       

      I am trying to use AbstractResource for this.

      I tried the DocumentStore and called the bean via 'jsfunction:action' but the data (just an array of bytes) downloads as a file in the browser.

      I don't want this to happen and I am not sure if it can be avoided either.

      I don't want the user be aware of the background GET request.

       

      Anybody tried to do something like this??

      For example let's assume that we just need to update some hidden fields on the page and the data is at the server.

      And we need to do it via ajax and don't want the user be aware of this process.

       

      Any help/pointers appreciated.

        • 1. http get request by jsfunction
          erbora00

          OK, I just found out that we can specify a URL in the action parameter of jsfunction.

          <a:jsFunction name="test" ajaxSingle="true"

                                                  action="/seam/resource/myresource" oncomplete="onTestEnd();">

          </a:jsFunction>

           

          Here I had to modify the getResourcePath method so that it returns the suffix as well.

          public String getResourcePath() {

                              return "/myresource.seam";

          }

           

          Now again same thing as DocumentStore; the data is downloaded as a file in the browser (Chrome).

           

          Can't this be done in the background?

          And the oncomplete callback never gets called with this method...

           

          I am writing the data using the HttpServletResponse object in the getResource method of AbstractResource.

           

          Below is the getResource method I use for testing:

          public void getResource(HttpServletRequest httpservletrequest,

                         HttpServletResponse response) throws ServletException, IOException {

          response.setContentType("application/octet-stream");

          response.setStatus(200);

           

          response.flushBuffer();

          ServletOutputStream out = response.getOutputStream();

          byte[] data = new byte[4096];

          for (int i = 0; i < 4096; i++)

                                        data[i] = (byte) i;

           

          out.write(data);

          response.flushBuffer();

          out.close();

          }

          • 2. http get request by jsfunction
            nbelaevski

            Hi,

             

            Why not simply use jQuery.get(...) for this?

            • 3. http get request by jsfunction
              erbora00

              Because I haven't used jQuery before.

              I will now learn and try it and then let you know the results.

              Thanks for the pointer.

              • 4. http get request by jsfunction
                erbora00

                Hi Nick

                 

                That works perfectly..

                Thanks very much..