Foros del Web » Programando para Internet » Javascript » Frameworks JS »

Procesar información Json

Estas en el tema de Procesar información Json en el foro de Frameworks JS en Foros del Web. Hola a todos, Necesito un archivo php para procesar la información (RobotServiceOld.php) pero por mas que intento no puedo hacer que funcione (apenas estoy aprendiendo). ...
  #1 (permalink)  
Antiguo 29/07/2012, 11:33
 
Fecha de Ingreso: noviembre-2003
Ubicación: Medellín-Colombia
Mensajes: 149
Antigüedad: 20 años, 5 meses
Puntos: 2
Procesar información Json

Hola a todos,


Necesito un archivo php para procesar la información (RobotServiceOld.php) pero por mas que intento no puedo hacer que funcione (apenas estoy aprendiendo). Mi código en javascript es el siguiente:

Código Javascript:
Ver original
  1. var loadingProcess = "";
  2. var existence;
  3. $(document).ready(function() {//On page load
  4.   $("#number").val("");
  5.     loadCities();
  6.     $("#number").keyup(changeNumber);
  7.     $("#number").focusout(changeNumber);
  8.     $("#search").button({icons: {primary: "ui-icon-search"}});
  9.     $("#search").attr("disabled", true);
  10.     $("#addProcess").button({icons: {primary: "ui-icon-check"}});
  11. });
  12.  
  13. function changeNumber() {
  14.     var size = $("#number").val().length;
  15.     $("#textCounter").html("(" + size + ")");
  16.     var disabled = true;
  17.     if(size == 23) {
  18.         disabled = false;
  19.     }
  20.     $("#search").attr("disabled", disabled);
  21. }
  22.  
  23. function loadCities() {
  24.     $.ajax({
  25.         type: "POST",
  26.         url: "php/RobotServiceOld.php",
  27.         dataType: "json",
  28.         data: {f:"gC"},
  29.         success: function(data) {
  30.             $("#cities").empty();
  31.             if(data == null) {
  32.                 $("#error-message").show("drop");
  33.             } else if(data!=""){
  34.                 $("#cities").append('<option value="">Seleccione uno</option>');
  35.                 for(var i = 0; i<data.length; i++) {
  36.                     $("#cities").append('<option value="' + data[i].value + '">' + data[i].name + '</option>');
  37.                 }
  38.                 $("#cities").removeAttr("disabled");
  39.                 $("#cities").change(citySelected);
  40.             }
  41.         },
  42.         error: function() {
  43.             $("#error-message").show("drop");
  44.         }
  45.     });
  46. }
  47.  
  48. function citySelected() {
  49.     var selectedCity = $("#cities").val();
  50.     $("#loading").hide();
  51.     $("#results").hide();
  52.     $("#process").hide();
  53.     $("#errorMessage").hide();
  54.     $("#notFound").hide();
  55.     $("#processName").val("");
  56.     $("#savingPanel").hide();
  57.     $("#corporations").empty();
  58.     $("#corporations").attr("disabled", true);
  59.     $("#corporations").append('<option>Cargando...</option>');
  60.     $("#number").attr("disabled", true);
  61.     $("#number").val("");
  62.     $("#search").attr("disabled", true);
  63.     if(selectedCity != "") {
  64.         $.ajax({
  65.             type: "POST",
  66.             url: "php/RobotServiceOld.php",
  67.             dataType: "json",
  68.             data: {f:"gCbC", c: selectedCity},
  69.             success: function(data) {
  70.                 $("#corporations").empty();
  71.                 if(data == null) {
  72.                     $("#error-message").show("drop");
  73.                 } else if(data!=""){
  74.                     $("#corporations").append('<option value="">Seleccione uno</option>');
  75.                     for(var i = 0; i<data.length; i++) {
  76.                         $("#corporations").append('<option value="' + data[i].value + '">' + data[i].name + '</option>');
  77.                     }
  78.                     $("#corporations").removeAttr("disabled");
  79.                     $("#corporations").change(corporationSelected);
  80.                 }
  81.             },
  82.             error: function() {
  83.                 $("#error-message").show("drop");
  84.             }
  85.         });
  86.     } else {
  87.         $("#corporations").empty();
  88.     }
  89. }
  90.  
  91. function corporationSelected() {
  92.     $("#loading").hide();
  93.     $("#results").hide();
  94.     $("#process").hide();
  95.     $("#errorMessage").hide();
  96.     $("#notFound").hide();
  97.     $("#processName").val("");
  98.     $("#savingPanel").hide();
  99.     if($("#corporations").val() != "") {
  100.         $("#number").removeAttr("disabled");
  101.     }
  102. }
  103.  
  104. function mostrarAyuda() {
  105.     $("#help").dialog();
  106. }
  107.  
  108. function clickSearch() {
  109.     $("#results").hide();
  110.     $("#loading").show();
  111.     $("#process").hide();
  112.     $("#errorMessage").hide();
  113.     $("#notFound").hide();
  114.     $("#processName").val("");
  115.     $("#savingPanel").hide();
  116.     $("#error-message-unhandled").hide();
  117.     var city = $("#cities").val();
  118.     var corporation = $("#corporations").val();
  119.     var number = $("#number").val();
  120.     if(number.length == 23) {
  121.         checkProcessExistence(city, corporation, number);
  122.         if(existence == true) {
  123.             searchProcess();
  124.         } else {
  125.             $("#loading").hide();
  126.             $("#repeated").dialog({
  127.                 modal:true,
  128.                 buttons: {Ok: function() {
  129.                     $(this).dialog("close");
  130.                 }}
  131.             });
  132.             return;
  133.         }
  134.     } else {
  135.         searchProcesses(1);
  136.     }
  137. }
  138.  
  139. function checkProcessExistence(city, corporation, number) {
  140.     $.ajax({
  141.         type: "POST",
  142.         url: "php/contenedor.php",
  143.         async: false,
  144.         cache: false,
  145.         dataType: "xml",
  146.         data: {f:"cPE", c: city, cp: corporation, n: number},
  147.         success: function(xml) {
  148.             if(xml == null) {
  149.                 $("#loading").hide();
  150.                 $("#error-message-unhandled").show("drop");
  151.             } else {
  152.                 if(xml.documentElement.getAttribute('c') != "0") {
  153.                     existence = false;
  154.                 } else {
  155.  
  156.                     existence = true;
  157.                 }
  158.             }
  159.         },
  160.         error: function() {
  161.             $("#loading").hide();
  162.             $("#error-message-unhandled").show("drop");
  163.         }
  164.     });
  165. }
  166.  
  167. function searchProcess() {
  168.     $("#resultsContent").html("").hide();
  169.     $("#results").hide();
  170.     $("#process").hide();
  171.     $("#errorMessage").hide();
  172.     $("#notFound").hide();
  173.     $("#processName").val("");
  174.     $("#savingPanel").hide();
  175.     var city = $("#cities").val();
  176.     var cityName = $("#cities option:selected").text().trim();
  177.     var corporation = $("#corporations").val();
  178.     var corporationName = $("#corporations option:selected").text().trim();
  179.     var number = $("#number").val();
  180.  
  181.     $.ajax({
  182.         type: "POST",
  183.         url: "php/RobotServiceOld.php",
  184.         dataType: "json",
  185.         cache: false,
  186.         data: {f:"lPbN", c: city, cp: corporation, n: number, v: "", ck: "", cpn: corporationName, cn: cityName},
  187.         success: function(json) {
  188.             $("#loading").hide();
  189.             if(json == null) {
  190.                 $("#error-message-unhandled").show("drop");
  191.             } else {
  192.                 if(json[0] == true) {
  193.                     $("#process").html(json[1]).show();
  194.                     $("#savingPanel").show();
  195.                 } else {
  196.                     $("#notFound").show("drop");
  197.                 }
  198.             }
  199.         },
  200.         error: function() {
  201.             $("#loading").hide();
  202.             $("#error-message-unhandled").show("drop");
  203.         }
  204.     });
  205. }
  206.  
  207. function searchProcesses(page) {
  208.     $("#savingPanel").hide();
  209.     $("#errorMessage").hide();
  210.     $("#process").hide();
  211.     if(page != 1) {
  212.         $("#resultsTable").fadeOut('fast');
  213.     }
  214.     var city = $("#cities").val();
  215.     var cityName = $("#cities option:selected").text().trim();
  216.     var corporation = $("#corporations").val();
  217.     var corporationName = $("#corporations option:selected").text().trim();
  218.     var number = $("#number").val();
  219.     var viewstate = "";
  220.     var cookie = "";
  221.     if($("#viewstate") && page != 1) {
  222.         viewstate = $("#viewstate").val();
  223.     }
  224.     if($("#cookie") && page != 1) {
  225.         cookie = $("#cookie").val();
  226.     }
  227.     $.ajax({
  228.         type: "POST",
  229.         url: "php/RobotServiceOld.php",
  230.         dataType: "html",
  231.         cache: false,
  232.         data: {f:"sPbPN", c: city, cp: corporation, n: number, p: page, v: viewstate, ck: cookie, cpn: corporationName, cn: cityName},
  233.         success: function(html) {
  234.             if(html == null) {
  235.                 $("#loading").hide();
  236.                 $("#error-message-unhandled").show("drop");
  237.             } else {
  238.                 $("#loading").hide();
  239.                 $("#resultsContent").html(html)
  240.                 $("#results").tabs().show("blind");
  241.                 $("#previous").button({icons: {primary: "ui-icon-triangle-1-w"}});
  242.                 $("#next").button({icons: {secondary: "ui-icon-triangle-1-e"}});
  243.             }
  244.         },
  245.         error: function() {
  246.             $("#loading").hide();
  247.             $("#error-message-unhandled").show("drop");
  248.         }
  249.     });
  250. }
  251.  
  252. function showLoading(sign) {
  253.     if(sign < 0) {
  254.         $("#loadingPrevious").show();
  255.     } else if(sign > 0) {
  256.         $("#loadingNext").show();
  257.     }
  258. }
  259.  
  260. function resetState() {
  261.     if($("#viewstate")) {
  262.         $("#viewstate").val("");
  263.     }
  264.     if($("#cookie")) {
  265.         $("#cookie").val("");
  266.     }
  267. }
  268.  
  269. function selectProcess(number) {
  270.     $("#process").hide();
  271.     $("#errorMessage").hide();
  272.     $("#notFound").hide();
  273.     $("#processName").val("");
  274.     $("#savingPanel").hide();
  275.     $("#loading_" + number).show();
  276.     loadingProcess = number;
  277.     var city = $("#cities").val();
  278.     var cityName = $("#cities option:selected").text().trim();
  279.     var corporation = $("#corporations").val();
  280.     var corporationName = $("#corporations option:selected").text().trim();
  281.     $.ajax({
  282.         type: "POST",
  283.         url: "php/RobotServiceOld.php",
  284.         dataType: "json",
  285.         cache: false,
  286.         data: {f:"lPbN", c: city, cp: corporation, n: number, v: "", ck: "", cpn: corporationName, cn: cityName},
  287.         success: function(json) {
  288.             if(json == null) {
  289.                 $("#loading_" + loadingProcess).hide();
  290.                 $("#error-message-unhandled").show("drop");
  291.             } else {
  292.                 $("#loading_" + loadingProcess).hide();
  293.                 $("#process").html(json[1]).show();
  294.                 $("#savingPanel").show();
  295.             }
  296.         },
  297.         error: function() {
  298.             $("#loading_" + loadingProcess).hide();
  299.             $("#error-message-unhandled").show("drop");
  300.         }
  301.     });
  302. }
  303.  
  304. function addProcess() {
  305.     var city = $("#cities").val();
  306.     var cityName = $("#cities option:selected").text().trim();
  307.     var corporation = $("#corporations").val();
  308.     var corporationName = $("#corporations option:selected").text().trim();
  309.     var number = $("#foundProcessNumber").val();
  310.     var name = $("#processName").val().trim();
  311.     if(name == "") {
  312.         alert("Debe ingresar un nombre para el proceso");
  313.         return;
  314.     }
  315.     $("#cities").attr("disabled", true);
  316.     $("#corporations").attr("disabled", true);
  317.     $("#number").attr("disabled", true);
  318.     $("#search").attr("disabled", true);
  319.     $("#addProcess").attr("disabled", true);
  320.     $("#loadingSave").show();
  321.     $.ajax({
  322.         type: "POST",
  323.         url: "php/RobotServiceOld.php",
  324.         dataType: "json",
  325.         cache: false,
  326.         data: {f:"sP", c: city, cp: corporation, n: number, d: name, cpn: corporationName, cn: cityName},
  327.         success: function(json) {
  328.             $("#cities").removeAttr("disabled");
  329.             $("#corporations").removeAttr("disabled");
  330.             $("#number").removeAttr("disabled");
  331.             $("#search").removeAttr("disabled");
  332.             $("#addProcess").removeAttr("disabled");
  333.             $("#loadingSave").hide();
  334.             if(json == null) {
  335.                 $("#error-message-unhandled").show("drop");
  336.             } else {
  337.                 if(json[0] == true) {
  338.                     $("#divForm").hide();
  339.                     $("#savingPanel").hide();
  340.                     $("#results").hide();
  341.                     $("#saveSuccess").show();
  342.                 } else {
  343.                     $("#errorMessageText").html(json[1]);
  344.                     $("#errorMessage").show();
  345.                 }
  346.             }
  347.         },
  348.         error: function() {
  349.             $("#error-message-unhandled").show("drop");
  350.         }
  351.     });
  352. }




Gracias de antemano por cualquier ayuda prestada.
  #2 (permalink)  
Antiguo 30/07/2012, 16:23
Avatar de dontexplain  
Fecha de Ingreso: junio-2012
Mensajes: 536
Antigüedad: 11 años, 10 meses
Puntos: 127
Respuesta: Procesar información Json

No voy a leer todo el código que has puesto, pero te diré que para convertir JSON válido a array que puedas manejar fácilmente basta con

array = $.parseJSON(textojson);

que devuelve el array que se puede manejar con

$.each(array,function(i,v){//funciones a ejecutar});

Un saludo
__________________
21añero.
HTML/CSS, PHP, JS/jQuery, Mysql; NodeJS/Socket.io (& V8); C++ ; Python (wxpy); Ensamblador.
Músico (clarinetista/pianista) y compositor

Etiquetas: ajax, funcion, html, js, json, php, procesar
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 02:58.