0

I am trying to make an AJAX Request to my function in my controller and it works, however, it returns me the whole page which i don't want. I have tried encoding it with json but no success i also have tried different formats in the url such as format=raw, format=ajax and format=json none of them worked and both format=raw and format=json gave me a 404 error.

So, what i am trying to do, is a request to a function in my controller that should return me "ok" but, instead of returning "ok", it gives me the whole HTML. I surely am missing something, i just don't know what it is. I have tried changing the URL parameter, dataType but still same result. Plus, i have seen some examples wihout the controller name in the url parameter;

example:

url: index.php?option=com_gallery&task=submit but it didn't work out for me nor did this: url: index.php?option=com_gallery&controller=send&task=submit; this is the only thing that has worked out for me: index.php?option=com_gallery&task=send.submit.

Any clarifications about these url parameters when it comes to AJAX requests to the controller and its methods?

My ajax request:

$.ajax(
    {   url: 'index.php?option=com_gallery&task=send.submit',    
        method: 'POST',
        dataType: 'text',       
        success: function(data){
            alert(data);
        },      
        error:function(){
            alert("error");
        },

        });
}

My controller send.php

defined('_EXEC') or die ('Restricted access');


class GalleryControllerSend extends JControllerForm
{


      public function submit($key=null, $urlVar=null)
      {
         echo "ok";        
      }

}

What should i do to display just the "ok" in the function submit() ?

The only thing that has worked out was adding format=html to the url parameter and adding exit(); below the echo "ok" but i believe that's not the best way to do it.

1

What you should do is include &format=json in your URL. This tells Joomla that you want a JSON response rather than HTML, and it won't run (subsequently) your template to produce all your page HTML.

The usual approach is to call

parent::display() 

in your controller code (ie in submit()). This will cause Joomla to look for a display() function in a view.json.php file with the same class name as in your view.html.php file. There you should be able to do:

echo new JResponseJson('ok');

The JResponseJson class will look after stopping the application at that point as there's no need to continue on.

For an example view.json.php file see https://docs.joomla.org/J3.x:Developing_an_MVC_Component/Adding_AJAX#site.2Fviews.2Fhelloworld.2Fview.json.php

I suggest you omit the datatype - jQuery will be able to figure it out.

| improve this answer | |
  • Yes you talked about JResponseJson but i tried to start off the the basics haha, could not get it working tho. Thank you! – Thiago Guimarães Apr 12 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.