my getJSON code doesn't work in jquery 1.4.2
This is my first question on stackoverflow. I just wonder why my getJSON code doesn't work with jQuery 1.4.2, it worked smoothly with jQuery 1.3.2 though
So here is my 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.');
}
}
The getJSON link is a CodeIgniter App Link. Anyone know why this 开发者_如何学Pythondoesn't work anymore?
The most likely cause if your JSON is not completely valid, this is now checked in jQuery 1.4+
From the docs:
jQuery 1.3 and earlier used JavaScript’s eval to evaluate incoming JSON. jQuery 1.4 uses the native JSON parser if available. It also validates incoming JSON for validity, so malformed JSON (for instance {foo: "bar"}) will be rejected by jQuery in jQuery.getJSON and when specifying “json” as the dataType of an Ajax request.
Use something like JSONLint to validate/fix your JSON, it should start working once valid. Take the response from '../admin/media_admin/ajaxHapus/'+id
and check it on JSONLint, you can also view it with FireBug which is handy.
getJson in jquery 1.4 not fire. Here is an example to solve this problem:
//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
}
});
精彩评论