Search code examples
javascriptjqueryjquery-post

JQuery $.post Not working on the second click


Thanks for checking this out:

What im doing is reading multiple values like name and class to determine the being clicked, then sending it as a paremeter to a PHP on my server so i can switch its class and name value.

This works great and switches the class the first time I click it, but the second time I click on the element, it does not work anymore. Looking at chrome's console, i can see the POST result being successful, but the $.post callback function is not being run anymore.

$('a').click(function() {

            if($(this).attr("name")=="Config"){
                return;
            }
            else if ($(this).attr("name")=="Return"){
                return;
            }
            else if ($(this).attr("name")=="Temperatura"){
                return;
            }

            else{



            var parameters = {};                            
                parameters.devicename   =   $(this).attr("name").split('_')[0];
                parameters.devicevalue  =   $(this).attr("name").split('_')[1];
                parameters.devicestate  =   parseInt($(this).html());

                alert (parameters.devicestate);

                var selector;

                if ($(this).hasClass("Luz_On") || $(this).hasClass("Luz_Off")){
                selector = "Luz";   
                $(this).attr('id','ajaxelement');
                alert("entro");
                alert($(this).attr('name'));
                alert(selector);
                alert($(this).attr('id'))
                }
                else if ($(this).hasClass("Cortina_Up") || $(this).hasClass("Cortina_Down")){
                selector = "Cortina";           
                $(this).attr('id','ajaxelement');                   
                }
                else if ($(this).hasClass("Temperatura")){
                selector = "Temperatura";
                $(this).attr('id','ajaxelement');
                }
                else if ($(this).hasClass("Rewind")){
                selector = "Rewind";
                $(this).attr('id','ajaxelement');
                }
                else if ($(this).hasClass("Play")){
                selector = "Play";
                $(this).attr('id','ajaxelement');
                }
                else if ($(this).hasClass("Pause")){
                selector = "Pause";
                $(this).attr('id','ajaxelement');
                }
                else if ($(this).hasClass("Forward")){
                selector = "Forward";
                $(this).attr('id','ajaxelement');
                }

            $.post(

            "http://localhost/AplicacionWeb/test.php", 

            parameters,

            function(data){

            if (data.valid=='true')
            {

                switch(selector)
                {
                    case "Luz":

                        alert("case");

                        if ($('#ajaxelement').attr('name') == "Luz_On"){

                        alert("caseif1");
                        $('#ajaxelement').removeClass("Luz_On").addClass("Luz_Off");
                        $('#ajaxelement').attr('name','Luz_Off');
                        $('#ajaxelement').removeAttr('id');

                        }
                        if ($('#ajaxelement').attr('name') == "Luz_Off"){

                        alert("caseif2");
                        $('#ajaxelement').removeClass("Luz_Off").addClass("Luz_On");
                        $('#ajaxelement').attr('name','Luz_On');
                        $('#ajaxelement').removeAttr('id');

                        }




                    break;

                    case "Cortina":

                    break;

                    case "Temperatura":

                    break;

                    case "Rewind":

                    break;

                    case "Play":

                    break;

                    case "Pause":

                    break;

                    case "Forward":

                    break;

                    default:
                        alert("C'est la vie");
                }




            }//data.valid

            }, 

            "json"

            );

            }



        });  // end click

Solution

  • You configured your post requests to expect the server, http://localhost/AplicacionWeb/test.php, to return valid JSON. If you verified that the request returned successfully, then check if the data conform to the specified dataType. jQuery ajax requests will not run the success/done callback if the response data is invalid.