I'm getting an ajax failed when working with symfony2 in ajax.
Any ideas?
HTML
$.ajax({
url: "{{ path('destination_app_ajax') }}",
type: "post",
data: {
"json" : JsonData
},
success: function(JsonData){
$('#result').html(JsonData);
/* Clear all Data */
JsonData = "";
localStorage.clear();
alert("Data Sent!");
},
error:function(){
alert('ajax failed');
}
});
Controller
public function ajaxAction(){
$json = json_encode($_POST);
return new Response(array('json' => $json));
}
JsonResponseobject class in Symfony2. more useful than manually encoding your response, and it sets appropriate headers etc. Additionally, you should look further into what error status you are getting from the server on the jQuery side -jQuery.ajax().error()has the following method signature:Type: Function(jqXHR jqXHR, String textStatus, String errorThrown). A status code can go a long way in debugging...