0

I finally made my AJAX working in Extbase Typo3 6. However, I'm trying to use the returned array from AJAX in my FLUID view.

my Ajax action in Controller:

public function ajaxAction(){
    $id = $this->request->getArgument['id'];
    $record = $this->articleRepository->findByUid($id);
    return json_encode(['record'=>$record,'status' => 'Loaded']);
}

My AJAX :

function dropCall(selectFieldObj) {
    $.ajax({
        type: 'POST',
        url: link,
        dataType: 'json',
        success: function(data){
            alert(data.status);
        }
    });

}

Now the question is how do I use the data object sent from the controller action in my View ? The alert is being displayed.

My List.html view

<script type="text/javascript">
    var link
            = '<f:uri.action action="ajax" controller="Article" pageType="99" arguments="{data:1}"/>';
</script>
<script type="text/javascript" src="fileadmin/myScript.js"></script>

<select id="drop" onchange="dropCall(this)">
    <option value="1">Apple</option>
    <option value="2">Mango</option>
    <option value="3">Grape</option>
</select>

In my view, I should be able to do something like

  <h1> {record.name} </h1>

EDIT

The object record might contain many properties and each record.property like record.name will be under <h1> tags and record.somethingelse can be under some under tag.

Just like we send objects from controller to view and use them in FLUID, I would want to do the same with AJAX

user2354302
  • 1,833
  • 5
  • 23
  • 35
  • Is you question about how to iterate over `data.record.whatever`, create new ` – Michael Aug 21 '13 at 08:42
  • I'm afraid that's not my aim. Check my edit please. Thank You :) – user2354302 Aug 21 '13 at 09:00
  • Ok, but that's not much difference actually. The model is already there (`data.record`), the view is the html which is already in the browser and the controller, well, you have to code some JavaScript I guess (`jQuery("#myH1").html(record.name)` and so on). If you want to make this a little bit easier, you can use a JS MVC-like helper like [backbone](http://backbonejs.org/). – Michael Aug 21 '13 at 09:17
  • Thank you for the answer. This would work if I were to give a `single record`, but what if I have `multiple records` in `record` contains all the DB Table, that's not very effeceint.. or is it? I dont know – user2354302 Aug 21 '13 at 10:41
  • Ok, then just return the data you need in the extbase controller, not the whole database content. And also, do not read just one record, but all you need from the database with `->findALl()` or a custom method on the repository. – Michael Aug 21 '13 at 11:31

0 Answers0