CÓDIGO JS
$("#getdata").click(function(e) { console.log("HEY"); var source = $('#origin').val(); console.log(source); var destination = $('#destination').val(); console.log(destination); e.preventDefault(); $.ajax({ type:'post', url:"{{url('/searchDistance')}}", dataType: 'json', data: jQuery("#distance_form").serialize(), success: function(data) { var x = JSON.parse(data); console.log(x); }, error: function(xhr, status, error) { console.log("Error"); console.log(error); } });Código del controlador PHP Estoy haciendo esto en laravel
public function search (Request $request) { if($request->ajax()) { $source = $request->get('from'); echo $source; $destination = $request->get('to'); echo $destination; $FindSource = google_api::where('source', '=', $source)->exists(); if(!$FindSource) { return response()->json(['success'=>false, 'message' => 'Source Not Found']); } else { $FindDestin = google_api::where('destination', '=', $destination)->first(); if(!$FindDestin) { return response()->json(['success'=>false, 'message' => 'destination not found']); } else { return response()->json(['success'=>true, 'message'=> 'success', 'data'=> $FindDestin]); echo json_encode($FindDestin); } } } }}
Recibo una respuesta adecuada en xhr y en la red, pero mi función de éxito no funciona y la función de error se ejecuta todo el tiempo con un error de SyntaxError: JSON.parse: carácter inesperado en la línea 1, columna 1 de los datos JSON.