Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

108
Views
Using Jquery and ajax post method for sending some data and then to fetch a response. response is coming properly, success function not working

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.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!