SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    57 Post(s)
    Tagged
    0 Thread(s)

    How does jQuery handle a server js response?

    Since prototype has apparently lost the js war I am beginning to familiarize myself with jQuery. One feature I'm fearful of losing is the ability of my server to respond to an Ajax request with a pure javascript response. When prototype receives such a response, it eval's it. What does jQuery do?

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,358
    Mentioned
    179 Post(s)
    Tagged
    9 Thread(s)
    Quote Originally Posted by Michael Morris View Post
    Since prototype has apparently lost the js war I am beginning to familiarize myself with jQuery.
    Excellent

    Quote Originally Posted by Michael Morris View Post
    One feature I'm fearful of losing is the ability of my server to respond to an Ajax request with a pure javascript response. When prototype receives such a response, it eval's it. What does jQuery do?
    I'm not overly familiar with this, but something like this should work, just fine:

    Code JavaScript:
    $.ajax({
      type : "POST",
      cache : false,
      url : "myScript.php",
      data  : 'variable1=' + var1 + '&variable2=' + var2,
      success : function(data) {
        script = data.replace(/<script>(.*)<\/script>/, "$1");
        eval(script);
      }
    });

  3. #3
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    57 Post(s)
    Tagged
    0 Thread(s)
    Close, but I am following the rules of the prototype framework. My response doesn't have script tags - instead it sends the appropriate header (text/javascript) to signal the file's purpose. Also, it may send that response even if it sends an error header, such as 403 denied.

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,358
    Mentioned
    179 Post(s)
    Tagged
    9 Thread(s)
    Oh, ok.
    Then just use the dataType: script setting.
    This evaluates the response as JavaScript and returns it as plain text.
    The script is also executed in the global context, so it can refer to other variables and use jQuery functions.

    Here's a slightly more explicit example:

    HTML Code:
    <!DOCTYPE HTML>
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <!--http://www.sitepoint.com/forums/showthread.php?983565-How-does-jQuery-handle-a-server-js-response-->
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>ajax() example</title>
        <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
      </head>
      
      <body>
        <form id="myForm">
          <input id="submit" type="submit" />
        </form>
      
        <script>
          $('document').ready(function () {
            $("#myForm").submit(function(e) {
              $.ajax({
                type : "POST",
                dataType:'script',
                url : "myScript.php"
              });
              e.preventDefault();
            });
          });
        </script>
      </body>
    </html>
    myScript.php

    PHP Code:
    <?php 
      
    echo "
        alert('Hello there'); 
        $('#submit').css('display', 'none')
      "
    ;
    ?>
    Does that help any?

    You might also want to look at:
    http://api.jquery.com/jQuery.ajax/
    http://api.jquery.com/jQuery.getScript/


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •