Calling Ajax does nothing

2

I have the following code, but when I click on the "Next" button, nothing happens, I can not find what my error would be.

javascript

$("#btnSigModif").click(function(){

    $.post("../Logica/getdataCargarDatosPreIns.php", {"NroSol":$("#NroSol").val()}, function(data){


        if(data.nombres){

            $("#nombres").val(data.nombres);

        }else{
            $("#nombres").val("error");
        }

    },"json");

    $("#datos").show('slow');
});

getdataCargarDatosPreIns.php

<?php
  $NroSol=$_POST["NroSol"];
  echo json_encode(array(
    "nombres"=>"si hay datos"
  );
?>

html

<input type="text" id="NroSol" name="NroSol"/>
<input type="button" id="btnSigModif" value="SIGUIENTE"/>

<div id="datos">
  <input type="text" name="nombres" id="nombres" />
</div>
    
asked by Pablo Chaco 12.06.2016 в 02:05
source

2 answers

5

Your error is on this line.

 echo json_encode(array(
   "nombres"=>"si hay datos"
    );

You are not closing the json_encode function.

It should be like this:

 echo json_encode(array(
   "nombres"=>"si hay datos"
   ));

Here I leave the example with ajax.

$('#btnSigModif').click(function(){
    $.ajax({
        url: "../Logica/getdataCargarDatosPreIns.php",
        type: "POST",
        dataType: "json",
        data: { NroSol: $("#NroSol").val() },
        success: function(data){
            if (data.nombres){
                $("#nombres").val(data.nombres);
            }else{
                $("#nombres").val("error");
            }
        }
    });
    $("#datos").show('slow');
});
    
answered by 12.06.2016 / 05:19
source
1

In the php code try to do this:

$NroSol = $_POST["NroSol"];

var_dump($NroSol);

So you can know how to show the content of the variable and you can know if it works correctly

And also to see if the ajax connection works well, try using the browser's web console and it shows you all the requests

Or you can try this way too:

$.ajax({
  method: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
})
.done(function( msg ) {
  alert( "Data Saved: " + msg );
});
    
answered by 14.06.2016 в 12:20