JS CODE
$("#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);
}
});
PHP Controller code I am MAking this in 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);
}
}
}
}
}
I am getting a proper response in xhr and network but my success function is not working and error function is running all the time with an error of SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data.