Last active
March 26, 2019 18:57
-
-
Save yphastos/314bc6e2fbf292a492f0 to your computer and use it in GitHub Desktop.
simple ajax with json response
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.ajax({ | |
url:"someUrl.php", | |
// async:false, // dependera del caso, | |
dataType:"json", // aqui indico que ya es json | |
type:'POST', | |
success:function(res) { | |
if(res.estatus){ | |
// todo bien, proseguir | |
} | |
else{ | |
// mostrar error | |
alert(res.mensaje); | |
} | |
}, | |
error:function(){ | |
alert("hubo un error, intente de nuevo"); | |
}, | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ref: http://api.jquery.com/jquery.ajax/ | |
$.ajax({ | |
url: "myAJAX.php", | |
async:false, //default:true. Para que funcione el blockUI debe ser true | |
// dataType:"json", // si uso esto, y regreso una cadena que representa un json, (en vez de, supongo, un json en si), causa conflictos | |
data: { | |
oper:"verificar_items", | |
folioOrden:folioOrden | |
}, | |
type: "post", //get | |
success: function(response) { | |
//se asume que la respuesta es un json. | |
try{ | |
res = $.parseJSON(response); | |
// console.log(res); | |
}catch(e){ | |
a = "Ocurrio un error."; | |
// a+= "error en json: "+e; | |
// a+= "\n\nDetalles:\n\n"+response; //cuidado, podria exponer informacion a prod | |
alert(a); | |
console.log(response); //no comentar para poder ver en la consola el response, aunque se puede ver en la peticion en firebug | |
return false; | |
} | |
// $.unblockUI(); | |
if(res.estatus == 0){ | |
//caso errror, e.g.: | |
m = res.mensaje; | |
alert(m); | |
error = 1; | |
}else{ | |
//caso exito, e.g.: | |
$("#some_div").html(res.datos); | |
$("#grid").setGridParam({data:datos}).trigger("reloadGrid"); | |
error = 0; | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment