tags:

views:

777

answers:

4

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 doesn't work anymore?

+7  A: 

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.

Also, welcome to SO :)

Nick Craver
thanks nick for the reply. i already try paste my code in jsonlint.com for validation. but it keeps tell me that parser failed.
Nikolius
@Nikolius - Can you post the JSON?
Nick Craver
this is my return json{'sukses':'1','id':56}
Nikolius
@Nikolius - Your JSON should be double quotes to be valid, like this: `{"sukses":"1","id":56}`
Nick Craver
@Nick - Thank you nick, finally can get it work with jquery 1.4.2. :D
Nikolius
@Nikolius - Welcome :) If this solved your problem, please accept by clicking the check-mark beside this so the next user finding it quickly sees the solution :)
Nick Craver
A: 

I have a similar problem stated in a separate post. My php script is generating JSON for my jquery. I just used JSONLint and it stated that the JSON is valid.

Yet getJSON fails to return information using jQuery 1.4.2 yet it has been working in production for over a year now with jQuery 1.3.2.

Here is copy of my post:

I have a production webpage working fine for over a year now. Recently, I wanted to upgrade to 1.4.2 in order to take advantage of the many new features. For some reason, my getJSON is causing IE 7 to deliver the following error: line 552 length is null or not an object

Firefox and Google Chrome also have the same problem.

In looking at the jQuery 1.3.2 and 1.4.2, there are differences in the getJSON code. Why would something work nicely in 1.3.2 and crash in 1.4.2?

The only code change in my HTML code is the library. Thanks. Sammy abcParsing

Microsoft Debugger Output nodeName: function( elem, name ) { return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase(); }, // args is for internal usage only each: function( object, callback, args ) { var name, i = 0, length = object.length, //line 552 is this one isObj = length === undefined || jQuery.isFunction(object); if ( args ) { if ( isObj ) { for ( name in object ) { if ( callback.apply( object[ name ], args ) === false ) { break; } } } else { for ( ; i < length; ) { if ( callback.apply( object[ i++ ], args ) === false ) { break; } } } // A special, fast, case for the most common use of each } else { if ( isObj ) { for ( name in object ) { if ( callback.call( object[ name ], name, object[ name ] ) === false ) { break; } } } else { for ( var value = object[0]; i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {} } } return object; }

The getJSON Function is listed below.

$.getJSON(php_program, // 1st arg to getJSON is the URI of the script { test_category:test_category, test_selected:test_selected, noise:noise_value, ph_noise:ph_noise_value, cal:cal_value, ph_cal:ph_cal_value, qt:qt_value, weeks_selected:[weeks_selected], years_selected:[years_selected], analysis_option:analysis_option, aborted_runs_option: aborted_runs_option, week_definition:week_definition, rf_database_option:rf_database_option }, function(wom) { $.each(wom,function(i,j) { //alert(j.wom+" "+j.header_and_results_noise_table); //alert(j.wom+" "+j.header_and_results_ph_noise_table); //alert(j.wom+" "+j.header_and_results_cal_table); //alert(j.wom+" "+j.header_and_results_ph_cal_table); //alert(j.wom+" "+j.header_and_results_qt_table); //alert(j.wom+" Total Counter_Noise: "+j.ds0_total_cntr); //alert(j.wom+" Total Counter_PH_Noise: "+j.ds1_total_cntr); //alert(j.wom+" Total Counter_CAL: "+j.ds2_total_cntr); //alert(j.wom+" Total Counter_PH_CAL: "+j.ds3_total_cntr); //alert(j.wom+" Total Counter_QT: "+j.ds4_total_cntr); noise_array.push([j.wom,j.Noise]); ph_noise_array.push([j.wom,j.PH_Noise]); cal_array.push([j.wom,j.Cal]); ph_cal_array.push([j.wom,j.PH_Cal]); qt_array.push([j.wom,j.QT]); var index=j.wom + "_Noise"; noise_totals_array[index]=j.ds0_total_cntr; index=j.wom + "_PH_Noise"; ph_noise_totals_array[index]=j.ds1_total_cntr; index=j.wom + "_Cal"; cal_totals_array[index]=j.ds2_total_cntr; index=j.wom + "_PH_Cal"; ph_cal_totals_array[index]=j.ds3_total_cntr; index=j.wom + "_QT"; qt_totals_array[index]=j.ds4_total_cntr; });

JSON Code Output with 1.3.2(Nothing gets printed with 1.4.2) Generated with php's json_encode [{"wom":16,"Noise":57,"ds0_total_cntr":100,"PH_Noise":71,"ds1_total_cntr":99,"Cal":86,"ds2_total_cntr":100,"PH_Cal":36,"ds3_total_cntr":99,"QT":85,"ds4_total_cntr":100},{"wom":17,"Noise":47,"ds0_total_cntr":100,"PH_Noise":56,"ds1_total_cntr":100,"Cal":84,"ds2_total_cntr":100,"PH_Cal":36,"

abcParsing
the JSON must be valid in 1.4.2, in my case i use single quote. when i change the JSON with double quote is just work fine in 1.4.2apparently using single quote is not a valid JSONhope this will help
Nikolius
A: 

haha,last I found answer here,thank you very much.一直很奇怪1.4.2 getjson不管用。

VZY
A: 

Thank you very much.

Dancho