Question

Ceci est ma première question sur stackoverflow. Je me demande pourquoi mon code getJSON ne fonctionne pas avec jQuery 1.4.2, il a travaillé en douceur avec jQuery 1.3.2 si

Alors, voici mon code

$(document).ready(function(){
    $('td.hps_ajax a').click(function() {
        id = this.id.replace(/.*hps_ajax/,'');
        if(confirm('Anda yakin mau menghapus record ini?'))
            $.getJSON('../admin/media_admin/ajaxHapus/'+id, remove_row);
        return false;   
    }); 
})

function remove_row(data) {
    if(data.sukses == '1') {
        $('td.hps_ajax a#hps_ajax'+data.id).closest('tr').fadeOut('slow',function() {
            $(this).remove();
        });
    } else {
        alert('Gagal menghapus File.');
    }
}

Le lien getJSON est un CodeIgniter Lien App. Quelqu'un sait pourquoi cela ne fonctionne plus?

Était-ce utile?

La solution

La cause la plus probable si votre JSON est pas tout à fait valide, ceci est maintenant cochée dans jQuery 1.4 +

A partir de la documentation :

  

jQuery 1.3 et utilisé précédemment la eval de JavaScript pour évaluer JSON entrant. jQuery 1.4 utilise l'analyseur JSON natif si elle est disponible. Il valide également JSON entrant pour la validité, si malformé JSON (par exemple {foo: « bar »}). Sera rejeté par jQuery dans jQuery.getJSON et lors de la spécification « JSON » comme dataType d'une requête Ajax

Utilisez quelque chose comme JSONLint pour valider / corriger votre JSON, il devrait commencer à travailler une fois valide. Prendre la réponse de '../admin/media_admin/ajaxHapus/'+id et vérifier sur JSONLint, vous pouvez également consulter avec Firebug qui est à portée de main.

Autres conseils

getJSON dans jquery 1.4 ne se déclenche pas. Voici un exemple pour résoudre ce problème:

//begin ( in jquery 1.3.2)
$.getJSON("/url",{id: 'xyz'}, function(json){
     //alert('');
}




// change to ( in jquery 1.4.1)

$.ajax({url: "/url",
  dataType: "text",    // notice: not type : JSON
  success: function(text) {
    json = eval("(" + text + ")"); 

    // do something with JSON
  }
});
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top