0

I have a jquery ajax request:

  var settingsContainer = {};
  settingsContainer.font_family = $j("#supercontainer").css("font-family");


  var settingsContainerJson = JSON.stringify(settingsContainer);

  var BASE = "<?php echo Request::Root(); ?>/";
  return $j.ajax
  ({
  type: 'PUT',
  dataType: 'JSON',
  data: { settingsContainer: settingsContainerJson },
  url: BASE+'pages/update/{{ $page_id }}',
  success: function(data)
  { 
    alert(data);
  }
  });

This goes to my routing:

Route::put("/pages/update/{id}", array('as'=>'ajax', 'uses'=>'PageController@putSave'));

And that goes to my controller:

public function putSave($id) {
    $a = json_decode(Input::get('settingsContainer'));
    return $a['font_family'];
}

This currently does not return anything in Laravel 4. If I remove the dataType: json in my jquery call, it does return something, so it seems to break with the JSON data processing somehow. Suggestions? thanks!

Ray
  • 167
  • 2
  • 13
  • Aren't you getting a 500 error or anything? – Israel Ortuño Jul 16 '13 at 08:36
  • No, it just doesn't return anything. If I replace return $a['font_family'] with return $a, it outputs 2. I am staring a little bit too long at this I guess, but I thought I could access $a as if it was an array after decoding it... – Ray Jul 16 '13 at 09:09
  • try returning from your controller with the correct headers and JSON structure : `return Response::json(array($a['font_family']));` – David Barker Jul 16 '13 at 14:53

1 Answers1

0

I think it's because you are using PUT in your ajax as the request method but Laravel uses POST for a PUT request and mimics the PUT request by sending a hidden field as:

<input type="hidden" name="_method" value="PUT" />

This is automatically generated/appended by Form::open() method in the form and since you are using ajax and json, you should add _method:'PUT' within your data and change the type:'PUT' to type:'POST' so it'll be available in the $_POST array and Laravel can do the work (As a PUT request). So, your data should be something like this:

{
    type: 'POST',
    dataType: 'JSON',
    data: { settingsContainer:settingsContainerJson, _method:'PUT' },
    // ...
}

Also check this thread about PUT, DELETE etc methods and browser support.

Community
  • 1
  • 1
The Alpha
  • 143,660
  • 29
  • 287
  • 307