Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>No real difference at first sight:</strong></p> <p>Assuming that your 'node' variable is just a node id, there is not much difference between the two. Semantically, both seem to perform a 'get' operation (even though they are technically post requests), as they just retrieve data without changing any state on the server (ok, that's another assumption).</p> <p>Security wise, there is no relevant difference either - both can be 'forged' easily and the difference boils down to the same as with the 'standard' GET vs. POST debate, that is, no difference except that the first exposes the parameters a wee bit more 'accessible' to the casual observer, as they are in plain sight within the URL.</p> <p><strong>But a 'convenience' difference within Drupal:</strong></p> <p>Within Drupal, the first version is often encountered because it can make use of the <a href="http://drupal.org/node/224170" rel="nofollow noreferrer">Wildcard Loader Arguments</a> functionality (introduced in Drupal 6). Let's say your callback URL is defined in hook_menu like so:</p> <pre><code>$items['module/get/%node'] = array( 'title' =&gt; 'Foo', 'type' =&gt; MENU_CALLBACK, 'page callback' =&gt; 'yourModule_callback', 'page arguments' =&gt; array(2), ); </code></pre> <p>With this, <code>yourModule_callback()</code> will be called with its first parameter already being the fully loaded node obect for the passed in nid, as <code>%node</code> tells Drupal to execute a <code>node_load()</code> on the parameter before handing it over to the callback function. Using the second version of your example, the callback function would have to load the node object itself, after extracting it from the POST data.</p> <p>So a convenience thing here.</p> <p>Additionally, a common pattern within Drupal is to use the same callback URL for an AJAX request as for its non javascript 'fallback' alternative. So when <code>yourModule_callback()</code> is invoked, it can do whatever it is intended to do with the passed in node first, basically assembling its result. After that is done, it only checks for <code>$ajax == TRUE</code> within the POST data. If it is there, it knows that the result is intended for an AJAX request, so it might output the result as a JSON. If it is not there, it knows that a full page cycle is going on, and can adjust its output accordingly (e.g. rebuilding a form, regenerating a whole page, whatever ...).</p> <p>That way, you don't have to register separate URLs for your non AJAX/AJAX callbacks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COThanks for explaining the difference between the two in detail. :) I see how both ways can be useful now. The way my form works is that it is generally intended for ajax, but if js is disabled, it will fallback to submitting the form with the form api.
      singulars
    2. COStill wondering if it is foolproof to simply check if $ajax==TRUE in my callback to determine if someone didn't just go to the url instead of using the form. For example, originally you go to "mysite/module/get/20" and you will trigger the callback (which I don't want to happen), but if you wrap the callback checking if $ajax==true you can prevent that. So that url won't trigger the callback anymore, and also can't use something like mysite/module/get/20&ajax=true to trigger the callback? Is there still tricky ways to bypass that, or should it be safe? Thanks.
      singulars
    3. COIt would not be 'foolproof'. It would only confirm that 'ajax = true' got posted to you, but not that this post actually came from your form. (Thus only preventing the 'easy' access via a standard GET). To ensure that the post itself came from the form, you have to use the formsAPI processing logic to rebuild the form and verify the form Token. This sounds like overkill for a simple AJAX callback - why are you concerned that someone can get the data without coming from the form? (If a user can access the form, ha can get at the AJAX response anyways)
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload