### Web Services - Sample 13: Using jQuery with the PyMOL web server

view page source quit pymol

PyMOL results will appear here

A simple jQuery and JSON function is $.getJSON. This function accepts a URL and the name of a JavaScript function to call after the URL result arrives.

    <A HREF="javascript:$.getJSON('http://localhost:8083/apply/pymol.cmd.load?filename=$PYMOL_PATH/test/dat/pept.pdb&_callback=?', callback1)">load</A>

causes this load link to appear. The callback1 function simply places the result into the black-border box above. You can view this callback1 function by using the "view page source" link above.

Next, let's delete this object and show another common way of using jQuery.

This html snippet

    <a href="javascript:void(0)" name=load>load</a>

defines a load link that is much simpler, putting the responsibilty on the jQuery $("a[name=load]").click property to define how to handle this link. This definition occurs in the jQuery $(document).ready function

    $(document).ready(function() {

      $("a[name=load]").click(function() {
        try {
          $.getJSON(host+"/apply/pymol.cmd.load?filename=$PYMOL_PATH/test/dat/pept.pdb&_callback=?", callback1);
        } catch (e) {
          alert(e);
        }
        return false; // causes the HREF to not load a new page
      });
    });

The full source can be seen by following the "view page source" link above.

Other links can be defined in a similar way, for example to get object names or to delete the object we loaded.

Sample16 shows how you can construct complex URL's containing multiple pymol commands. These URL strings can be used in the $.getJSON function shown above.

jQuery and cross-domain scripting

This document came from
as shown in the URL location bar of this browser. The jQuery requests are made to the PyMOL server at
This is a cross-domain request that violates the same-origin policy. But jQuery uses a workaround called cross-domain scripting with JSONP that allows a callback to process any response from the PyMOL server. In order to illustrate the necessity of a callback in this situation, click on this link to get an error alert.
badload