Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use Ajax with jQuery in Laravel 4?
    text
    copied!<p>I am brand new to Laravel 4 and seeing if I can convert my website over to it (previously written without a framework). I can't get AJAX (via jQuery) to interact properly with my controller.</p> <p>So firstly, the controller I am working in is called <code>IndexController.php</code> and has a function called <code>types</code> which is shown below:</p> <pre><code>class IndexController extends BaseController { // Other controller functions public function types($brand) { $types = DB::select('select * from aircraft_types where brand_id = ?', array($brand)); echo json_encode($types); } } </code></pre> <p>The function returns the data from the database (in JSON format). I have then created a route to this controller and function as follows:</p> <pre><code>Route::get('types/{id}', array('uses'=&gt;'IndexController@types')); </code></pre> <p>I have doubled checked that the route and function are working by going to //localhost/lavarel/public/types/1 and indeed it returns the correct JSON data.</p> <p>The jquery function in question is below:</p> <pre><code>function updateTypes($brand) { $("#type").append('&lt;option value="test"&gt;Test...&lt;/option&gt;'); // This executes correctly $.getJSON('/types/'+$brand, function(data) { $("#type").append('&lt;option value="test 2"&gt;Test 2...&lt;/option&gt;'); // This does not // Some other JSON related code }); </code></pre> <p>To test where the function works, I have inserted two lines which edit the item I am using. The function is being called correctly as the first 'Test' option is being appended. However, it never seems to activate the callback function as the second line of test code is not executed.</p> <p>I suspect the issue is the URL I am providing to JavaScript <code>'/types/'+$brand</code>. I have seen in some tutorials a BASE var used before the URL I provide, but I don't see why my code above wouldn't work. Any pointers?</p> <p>Can anyone explain to me where I am going wrong?</p>
 

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