{"id":141208,"date":"2018-09-03T13:05:09","date_gmt":"2018-09-03T13:05:09","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=141208"},"modified":"2020-05-27T10:37:08","modified_gmt":"2020-05-27T10:37:08","slug":"ajax-call-from-plugins-and-modules-in-joomla","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/ajax-call-from-plugins-and-modules-in-joomla\/","title":{"rendered":"Ajax Call from Plugins and Modules In Joomla"},"content":{"rendered":"<div class=\"panel panel-primary\">\n<div class=\"panel-heading\">\n<div>\n<h3 class=\"panel-title\">Introduction<\/h3>\n<\/div>\n<\/div>\n<div class=\"panel-body\">\n<p>Ajax makes it possible to create websites that are dynamic and fast.<br \/>\nWe can use Ajax to refresh a specific section of a page without reloading all the content or to validate single element without submitting the whole form.<\/p>\n<p>While developing an extension for Joomla we may encounter a task which requires jQuery Ajax call it may be for the dynamically changing content of an element or to get data based on user input or to perform some operation in the background.<\/p>\n<p>If we are developing a component then we do not have any issue as we can make Ajax request to any controller of the component but what if we need to create only a plugin or module we might stuck here.<\/p>\n<p>As in earlier Joomla version, we need to create our own component if we need Ajax call but Joomla! 3.2 includes an AJAX Interface.<\/p>\n<p>Basically, com_ajax is a component which serves as an interface for Ajax calls for standalone plugins and modules.<\/p>\n<\/div>\n<\/div>\n<div>\n<div class=\"panel panel-primary\">\n<div class=\"panel-heading\">\n<div>\n<h3 class=\"panel-title\">Some Use Cases<\/h3>\n<\/div>\n<\/div>\n<div class=\"panel-body\">\n<div>\n<ul>\n<li>First, A module\/plugin that retrieves data from an external API<\/li>\n<li>Second, A module\/plugin that interacts with a component that you did not develop<\/li>\n<li>Third, A module\/plugin that requires data using Ajax, we can write ajax method in same module\/plugin<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div>\n<div class=\"panel panel-primary\">\n<div class=\"panel-heading\">\n<div>\n<h3 class=\"panel-title\">How does Ajax Call work for modules and plugins?<\/h3>\n<\/div>\n<\/div>\n<div class=\"panel-body\">\n<div>\n<p>First of all, when\u00a0making Ajax call from module\/plugin URL must be, index.php?option=com_ajax.<\/p>\n<p><b>Other Parameters:<\/b><\/p>\n<p><b>Required &#8211; <\/b><\/p>\n<p>option=com_ajax<\/p>\n<p>[module|plugin]=name<\/p>\n<p>format=[json|debug|raw]<\/p>\n<p><b>Optional &#8211; <\/b><\/p>\n<p>method=[method name] defaults is get if not provided.<\/p>\n<p>A request to ?option=com_ajax&amp;module=search would call mod_search with results returned in the default format. Similarly ?option=com_ajax&amp;plugin=search&amp;format=json would trigger the onAjaxSearch with results returned in JSON.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div>\n<div class=\"panel panel-primary\">\n<div class=\"panel-heading\">\n<div>\n<h3 class=\"panel-title\">com_ajax for Modules<\/h3>\n<\/div>\n<\/div>\n<div class=\"panel-body\">\n<div>\n<p>For using Ajax call in Modules some requirements are:<\/p>\n<ol>\n<li>A method must be defined in helper.php<\/li>\n<li>The request must include a module variable in the URL.Example:module=search for mod_search<\/li>\n<li>Optionally method variable can be used in the URL to replace default get method. Example: method=myFunction this will trigger myFunctionAjax<\/li>\n<\/ol>\n<p><b>Example Request<\/b><\/p>\n<pre class=\"brush:php\">jQuery.ajax({\n    url: 'index.php?option=com_ajax&amp;module=search&amp;method=getData',\n    type: \"post\",\n    success :function(response){\n        console.log(response);\n    }\n});\n<\/pre>\n<p>Above code will trigger method getDataAjax in helper.php of search module.<\/p>\n<pre class=\"brush:php\">public static getDataAjax(){\n    \/\/search records\n}\n<strong>Note<\/strong>: function must be static otherwise warnings will be thrown.\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div>\n<div class=\"panel panel-primary\">\n<div class=\"panel-heading\">\n<div>\n<h3 class=\"panel-title\">com_ajax for Plugins<\/h3>\n<\/div>\n<\/div>\n<div class=\"panel-body\">\n<div>\n<p>For using Ajax call in plugins some requirements are:<\/p>\n<ol>\n<li>The method name must start with onAjax. Example: onAjax[Name]<\/li>\n<li>The request must include a plugin variable in the URL.Example:plugin=getData this will trigger onAjaxgetData<\/li>\n<li>Optionally group variable can be used in the URL to specify plugin group.<\/li>\n<\/ol>\n<p><b>Example Request<\/b><\/p>\n<pre class=\"brush:php\">\/**\n * WKSOCIALMESSAGE -Joomla Social Messaging\n *\n *\n * @category   Plugin\n * @package    Joomla\n * @author     WebKul software private limited \n * @copyright  2010 WebKul software private limited\n * @license    http:\/\/www.gnu.org\/licenses\/gpl-2.0.html GNU\/GPL\n * @version    GIT:1.0\n * @filesource http:\/\/store.webkul.com\n * @link       Technical Support:  webkul.uvdesk.com\n *\/\njQuery.ajax({\n    url: 'index.php?option=com_ajax&amp;group=system&amp;plugin=connectWksocialTwitterProfile&amp;format=json',\n    data:{wksocial_action:\"validate\"},\n    type: \"post\",\n    success: function(data){\n        \/\/handle success\n    },\n    error:function(data){\n        \/\/handle error\n    }\n)};\n\n<\/pre>\n<p>Above code will trigger method onAjaxConnectWksocialTwitterProfile in the plugin file.<\/p>\n<pre class=\"brush:php\">\/**\n * [onAjaxConnectTwitterProfile trigger onAjax for login user profile]\n *\n * @since  v1.0\n * @return [string] [content to render ]\n *\/\npublic function onAjaxConnectWksocialTwitterProfile()\n{\n   \/\/login user via twitter\n}\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Ajax makes it possible to create websites that are dynamic and fast. We can use Ajax to refresh a specific section of a page without reloading all the content or to validate single element without submitting the whole form. While developing an extension for Joomla we may encounter a task which requires jQuery Ajax <a href=\"https:\/\/webkul.com\/blog\/ajax-call-from-plugins-and-modules-in-joomla\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":136,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1808,4],"tags":[250,7447],"class_list":["post-141208","post","type-post","status-publish","format-standard","hentry","category-ajax","category-joomla-2","tag-ajax","tag-ajax-request"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Ajax Call from Plugins and Modules In Joomla - Webkul Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/ajax-call-from-plugins-and-modules-in-joomla\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Ajax Call from Plugins and Modules In Joomla - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"Introduction Ajax makes it possible to create websites that are dynamic and fast. We can use Ajax to refresh a specific section of a page without reloading all the content or to validate single element without submitting the whole form. While developing an extension for Joomla we may encounter a task which requires jQuery Ajax [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/ajax-call-from-plugins-and-modules-in-joomla\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-09-03T13:05:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-05-27T10:37:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Arjun Singh\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Arjun Singh\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/ajax-call-from-plugins-and-modules-in-joomla\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/ajax-call-from-plugins-and-modules-in-joomla\/\"},\"author\":{\"name\":\"Arjun Singh\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/22c9fe3ff42bc7be863f952c9670cb9a\"},\"headline\":\"Ajax Call from Plugins and Modules In Joomla\",\"datePublished\":\"2018-09-03T13:05:09+00:00\",\"dateModified\":\"2020-05-27T10:37:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/ajax-call-from-plugins-and-modules-in-joomla\/\"},\"wordCount\":448,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"ajax\",\"ajax request\"],\"articleSection\":[\"Ajax\",\"Joomla\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/ajax-call-from-plugins-and-modules-in-joomla\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/ajax-call-from-plugins-and-modules-in-joomla\/\",\"url\":\"https:\/\/webkul.com\/blog\/ajax-call-from-plugins-and-modules-in-joomla\/\",\"name\":\"Ajax Call from Plugins and Modules In Joomla - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2018-09-03T13:05:09+00:00\",\"dateModified\":\"2020-05-27T10:37:08+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/ajax-call-from-plugins-and-modules-in-joomla\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/ajax-call-from-plugins-and-modules-in-joomla\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/ajax-call-from-plugins-and-modules-in-joomla\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Ajax Call from Plugins and Modules In Joomla\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/22c9fe3ff42bc7be863f952c9670cb9a\",\"name\":\"Arjun Singh\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/fe0556551e188338dece8803a0e8011ee10c9de07e107a91b0a5a8023e8a0894?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/fe0556551e188338dece8803a0e8011ee10c9de07e107a91b0a5a8023e8a0894?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Arjun Singh\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/arjun-singh732\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Ajax Call from Plugins and Modules In Joomla - Webkul Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/ajax-call-from-plugins-and-modules-in-joomla\/","og_locale":"en_US","og_type":"article","og_title":"Ajax Call from Plugins and Modules In Joomla - Webkul Blog","og_description":"Introduction Ajax makes it possible to create websites that are dynamic and fast. We can use Ajax to refresh a specific section of a page without reloading all the content or to validate single element without submitting the whole form. While developing an extension for Joomla we may encounter a task which requires jQuery Ajax [...]","og_url":"https:\/\/webkul.com\/blog\/ajax-call-from-plugins-and-modules-in-joomla\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2018-09-03T13:05:09+00:00","article_modified_time":"2020-05-27T10:37:08+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Arjun Singh","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Arjun Singh","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/ajax-call-from-plugins-and-modules-in-joomla\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/ajax-call-from-plugins-and-modules-in-joomla\/"},"author":{"name":"Arjun Singh","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/22c9fe3ff42bc7be863f952c9670cb9a"},"headline":"Ajax Call from Plugins and Modules In Joomla","datePublished":"2018-09-03T13:05:09+00:00","dateModified":"2020-05-27T10:37:08+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/ajax-call-from-plugins-and-modules-in-joomla\/"},"wordCount":448,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["ajax","ajax request"],"articleSection":["Ajax","Joomla"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/ajax-call-from-plugins-and-modules-in-joomla\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/ajax-call-from-plugins-and-modules-in-joomla\/","url":"https:\/\/webkul.com\/blog\/ajax-call-from-plugins-and-modules-in-joomla\/","name":"Ajax Call from Plugins and Modules In Joomla - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2018-09-03T13:05:09+00:00","dateModified":"2020-05-27T10:37:08+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/ajax-call-from-plugins-and-modules-in-joomla\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/ajax-call-from-plugins-and-modules-in-joomla\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/ajax-call-from-plugins-and-modules-in-joomla\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Ajax Call from Plugins and Modules In Joomla"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/22c9fe3ff42bc7be863f952c9670cb9a","name":"Arjun Singh","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/fe0556551e188338dece8803a0e8011ee10c9de07e107a91b0a5a8023e8a0894?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/fe0556551e188338dece8803a0e8011ee10c9de07e107a91b0a5a8023e8a0894?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Arjun Singh"},"url":"https:\/\/webkul.com\/blog\/author\/arjun-singh732\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/141208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/136"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=141208"}],"version-history":[{"count":27,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/141208\/revisions"}],"predecessor-version":[{"id":251428,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/141208\/revisions\/251428"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=141208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=141208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=141208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}