0
votes

I've been trying for ages to get Json working in Joomla and I just can't do it. I think I've tried every combination of URL etc so any help would be great:

this is for the admin side structure looks like admin

-controllers

--orderitem.php

-views

--orderitem

---tmpl

----orderitem.php

-controller.php

function updateNow(newrefresh) {

var dataJSON = JSON.encode (newrefresh);
var request = new Request.JSON({
    method: 'post',

    url: 'index.php?option=com_customersitedetails&view=orderitem&task=refreshscreen&format=raw',   
    data: {
        json: dataJSON
        },
    onComplete: function(jsonObj) {
        alert("Your form has been successfully submitted ");

    }
}).send();

};

Although runs the alert box it doesn't retun JSON just

View not found [name, type, prefix]: orderitem, raw, customersitedetailsView

Any ideas where I can start? thanks

2
What are you trying to connect to? A function in the orderitem controller or to load the view orderitem or to call a function in controller.php? - David Fritsch
Take a look at com_finder. There's several AJAX parts in it that may give you some suggestions to fix your issue. - Michael
I want to call a function in a controller so I can put in JSON processing and return processed data - user1616338

2 Answers

0
votes

You're missing views/orderitem/view.raw.php containing a CustomersitedetailsViewOrderitem class.

views/orderitem/view.raw.php

class CustomersitedetailsViewOrderitem extends JViewLegacy
{
    public function display($tpl = null)
    {
        $response = 'Your magic response here';
        echo $response;
        JFactory::getApplication()->close();
    }
}
-1
votes

You can look here for proper ajax call in joomla How to Write PHP in AJAX

inside your controllers you should have a file "mycall.json.php" this file will process and return a json format of your ajax call

Joomla doesn't gives a build in AJAX as part of it's system. my answer is from Josef Leblanc course in lynda.com http://www.lynda.com/Joomla-1-6-tutorials/Joomla-1-7-Programming-and-Packaging-Extensions/73654-2.html

As I said : Write this i the frontend JS :

$.ajax({
        type: 'GET',
        url: 'index.php', 
        data: {option: 'com_componenetname', task: 'taskname.youroperation', format: 'json', tmpl: 'raw'},
    dataType: 'json',
        async: true, // can be false also
        error: function(xhr, status, error) {
                console.log("AJAX ERROR in taskToggleSuceess: ")
                var err = eval("(" + xhr.responseText + ")");
                console.log(err.Message);
                },
        success: function(response){

                // on success do something
                // use response.valuname for server's data
                        }
                ,
        complete: function() {
            // stop waiting if necessary 
                 }                     
          });

in the backend you should have a file under com_componentname/controllers/taskname.json.php

the file should look like this

class ComponentnameControllerTaskname extends JControllerLegacy (Legacy only J3.0)
{
    public function __construct($config = array())
    {
        parent::__construct($config);

        $this->registerTask('operationname', 'functionname');
    }

    public function functionname() {

            // do something in backend
            echo json_encode(array(''var1' => val1, 'var2' => val2 ) );
        }

}

nibra - I use this in all my joomla sites and its working perfect. your comment was wrong, pease give me my credit back