I have a modal (yahoo bootstrap) that I populate and post with ajax. Everything is going fine up until I try grab the input in my controller and Input::all() and $_POST[] is empty.
Im not getting any errors, all my routes are looking fine (I'm getting the wanted controllers) and I'm not using any filters (the token is just present for future use).
Originally I found the problem because my form validation came back with error regardless of my input. The validation part have been removed for simplicity and testing purposes.
My ajax:
<script type="text/javascript">
$(document).ready(function() {
$(' #modalSubmit ').click(function(e) {
e.preventDefault();
var myData = $('#myModalForm').serialize();
// alert(myData); //example: title=test&desc=something&_token=jhadskljhfaksjhfjksadhkfjh (just made up the token for the example)
$.ajax({
type: "post",
cache: false,
dataType: "json",
data: myData,
contentType: 'application/json; charset=UTF-8',
url: "http://tdc.dev/ajax"
})
.success(function( data ) {
$( "#myModal" ).html(data['body']);
$( "#myModalLabel" ).html(data['title']);
});
});
return false;
});
</script>
Relevant routes:
Route::get('/ajax', array(
'as' => 'ajax',
'uses' => 'AjaxTestController@getCreate'
));
Route::post('/ajax', array(
'as' => 'ajax',
'uses' => 'AjaxTestController@postCreate'
));
My AjaxTestController:
class AjaxTestController extends BaseController {
public function getCreate()
{
return Response::json(array('body' => View::make('audit.create')->render(), 'title' => 'My Title1'));
}
public function postCreate()
{
$title = Input::get('title'); // try to get title from the form
/*
| try to check for title in Laravel
*/
if (Input::has('title'))
{
$title = 'test';
} else {
$title = 'No title';
}
/*
| Is anything being posted?
*/
if (empty($_POST))
{
$title = 'Nothing in $_POST';
}
// $title = 'sdfasdfasf'; // to check that I'm getting the response from the controller.
return Response::json(array('body' => View::make('audit.create')->render(), 'title' => $title));
}
}
The different elements in the controller have been tested separately so they don't over rule one another and the response follows the if-statements as planned but with no input from the form
Any suggestions?
Your Laravel code is not the problem here. The problem may rather be in your JavaScript code that send the ajax request...
Try to remove the dataType and contentType options so that your ajax options look like that: $.ajax({type: "POST", data: myData, url: "http://tdc.dev/ajax"}); - you are not sending JSON content, but content encoded as "x-www-form-urlencoded" (which is the default content type).
( If you are using Chrome/Firefox, look in the developer tools to the network monitor and watch if your ajax request is correctly send to the server )
I'm not happy to remove the dataType: "json". I know, that I'm not sending json, but i need to recieve a response to my post in json... and I actually thought that dataType was the data format for the response and not the post/get format.
But still, not having anything else to go from I'll give it af try :-).
Thanks a bunch. Got a little confused in my response there. It was indeed the "contentType: 'application/json;". I changed that to contentType: 'charset=UTF-8' and now it works like a charm :-)
Scratch that! I removed contentType all together and it works!
Glad I could help you... ^^
NB: For UTF-8 encoding, from the jQuery docs:
POST data will always be transmitted to the server using UTF-8 charset, per the W3C XMLHTTPRequest standard
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community