javascript - can't figure out why ajax json request to server fails -
i learning how make ajax request jquery , json. have following code:
jquery
$(document).ready(function(){ $(document).on('submit','#form', function(e){ e.preventdefault(); var nombre = $("#nombre").val(); var apellido = $("#apellido").val(); alert(nombre+apellido); sendajax(nombre,apellido); }); function sendajax(nombre,apellido){ $.ajax({ data: { "nombre": nombre, "apellido": apellido }, contenttype:"application/json", datatype: 'json', url: 'ajax/ajax.php', type: 'post' }) .done (function(response) { alert(response.nombre); }) .fail (function() { alert("failed"); }); } });
**ajax.php**
if(isset($_post['nombre'])){ $json_data = array ('nombre' => $_post['nombre'] , 'apellido' => $_post['apellido']); header('content-type: application/json; charset=utf-8'); echo json_encode(json_data); }
as can see i'm not passing inputs values form, expecting alert string "nombre" (only testing purposes). request keeps failing , can't figure out why. wrong in code?
Comments
Post a Comment