Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

106
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda