Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to use json response properly
    primarykey
    data
    text
    <p>Im using <a href="https://datatables.net/usage/i18n" rel="nofollow noreferrer">datatables</a> to show data and I want to fetch language settings from my server in a nice way. The problem is to format the data in a way that makes this possible, I would prefer to use json format with as few modifications as possible. </p> <p>I want to use this: </p> <p>(as an argument for my datatable creation, what is this? array? object? json string?)</p> <pre><code>var oLanguage = { "oLanguage": { "sLengthMenu": "_MENU_ per page" } }; </code></pre> <p>I try to obtain this data from the server using this method:</p> <pre><code>$.getJSON( '/olan', function(data){ oLanguage=data; }); </code></pre> <p>SERVER CODE: (PHP, <a href="http://www.slimframework.com/" rel="nofollow noreferrer">SLIM</a>)</p> <pre><code>$app-&gt;get('/olan', function() use ($app, $lan){ $oLanguage = array('oLanguage' =&gt; array('sLengthMenu' =&gt; '_MENU_ per page')); $response = $app-&gt;response(); $response['Content-Type'] = 'application/json'; $response-&gt;body(json_encode($oLanguage)); }); </code></pre> <p>I was able to iterate through the data in the $.getJSON method and all information is there, must be something with the format/object handling. I don't yet have a good understanding of the type of object that i need, i can declare it, but i don't know what it is:) I hope its some kind of json object so i can just easily get it as i try to do. I could maybe declare it using $.each iteration but it would be ugly.</p> <p>This is my first question here on stackoverflow, thank you for helping me out</p> <p>-------------------------Answer (cannot post it the first 8 hours)---------------</p> <p>Turns out, </p> <blockquote> <p>thanks to <a href="https://stackoverflow.com/users/1091281/shinosha">Shinosha</a></p> </blockquote> <p>, that there is a build in feature of datatables to get the language settings from server. By creating the table like this:</p> <pre><code>$('#tableid').dataTable({ "oLanguage": { "sUrl": "/olan" } }); </code></pre> <p>and editing my server code: (changing the $oLanguage variable)</p> <pre><code>$app-&gt;get('/olan', function() use ($app, $lan){ $oLanguage = array('sLengthMenu' =&gt; '_MENU_ per page'); $response = $app-&gt;response(); $response['Content-Type'] = 'application/json'; $response-&gt;body(json_encode($oLanguage)); }); </code></pre> <p>It works!</p> <blockquote> <p><em>In Javascript, [] denotes array and {} denotes object. You can nest objects and arrays as required. Use a json validator like jsonlint.com to validate your server response.</em> , <a href="https://stackoverflow.com/users/1825844/gvmani">gvmani</a></p> </blockquote> <p>oLanguage is an object with the attribute oLanguage which is an object with the attribute sLengthMenu. sLengthMenu have the value "<em>MENU</em> per page"</p> <pre><code>var oLanguage = { "oLanguage": { "sLengthMenu": "_MENU_ per page" } }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload