Skip to content

Instantly share code, notes, and snippets.

@piotr-cz
Created September 20, 2013 10:28
Show Gist options
  • Select an option

  • Save piotr-cz/6635698 to your computer and use it in GitHub Desktop.

Select an option

Save piotr-cz/6635698 to your computer and use it in GitHub Desktop.

Revisions

  1. piotr-cz created this gist Sep 20, 2013.
    64 changes: 64 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    $.ajax({
    type : 'POST',
    data : request,

    // Got HTTP response code 200
    success: function (response)
    {
    switch (this.data.request.format)
    {
    // Handling JSON format
    case 'json':
    if (response.success)
    {
    // Do something with response.data
    }
    else
    {
    /* We got here because:
    * a) developer didn't use proper query string
    * b) called module/ plugin threw and exception
    *
    * Error message for debugging is in response.error.message;
    */
    alert('Contact developer of this extension');
    }

    // There are some messages to show
    if (response.messages)
    {
    Joomla.renderMessages(reponse.messages);
    }
    break;

    case 'raw':
    // Update the element with received data
    break;

    case 'image':
    // Update the image with received data blob
    break;

    case 'xml':
    // Parse XML
    break;

    default:
    // This extension cannot handle other formats.
    break;
    }
    }

    // Any format but JSON
    /* We got here because
    * a) developer didn't use proper query string
    * b) called module/ plugin threw and exception
    *
    * Error message for debugging is in response.responseText.
    * Additionally may check for HTTP status code: response.responseCode
    */
    error: function(response)
    {
    alert('Contact developer of this extension');
    }