Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts

    cant parse returned JSON

    hi, I have a call to a php script which return a JSON encoded array, I use similar other routines without issue, but for some reason this particular one will not work...
    Code:
    function get_json(str){
    	var i = jsc_sajax_call_var(str); //calls the php script which returns the json
    	try {
    		v = JSON.parse(i);
    		return v;
    	} catch (exception) {
    		alert(i);
    	}
    }
    function status_check(){
    	var str='?_f=fi_status_check&val_no={$vm_id}';
    	var v = get_json('val_main_loader.php'+str);
    	if(v){
    		alert(v._status_true);
    	}
    }
    if I alert() v , I get this..

    Code:
    {"_status_true":["load_option_tab_stones_1","load_option_tab_stones_1","load_option_tab_stones_2","load_option_tab_stones_2","load_option_tab_pearls_1","load_option_tab_pearls_1","load_option_tabs_costs_1","load_option_tab_edit_1","load_option_tab_edit_2"],"_status_false":["load_option_tab_pearls_1","load_option_tab_pearls_2","load_option_tabs_costs_2"]}
    but if I alert or try to iterate though v._status_false or v._status_true , I simply get 'undefined'

    I cant see what I am doing differently to other parts of the script which do similar things and work as expected ... though the format of this return is slightly different, am I accessing it incorrectly ?
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Did you check your console or the network tab of your developer tools to check whether

    a) the synchronous "A"jax has been executed
    b) the parameters have been correctly transferred to the PHP script
    c) the return value is as expected?

    If (and only if) the return value is exactly the same as you mentioned, it should work. You can try with this code
    Code:
    var v = {"_status_true":["load_option_tab_stones_1","load_option_tab_stones_1","load_option_tab_stones_2","load_option_tab_stones_2","load_option_tab_pearls_1","load_option_tab_pearls_1","load_option_tabs_costs_1","load_option_tab_edit_1","load_option_tab_edit_2"],"_status_false":["load_option_tab_pearls_1","load_option_tab_pearls_2","load_option_tabs_costs_2"]};
    console.log(v._status_true);
    If this works for you (it should), then the return value must be different.

  • Users who have thanked devnull69 for this post:

    firepages (10-15-2013)

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,473
    Thanks
    13
    Thanked 361 Times in 357 Posts
    but if I alert or try to iterate though v._status_false or v._status_true , I simply get 'undefined'
    can you show us the code that fails?

    I didn’t have any problems calling v._status_true in FireBug.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    calling

    Code:
    console.log(v);
    console.log(v._status_true);
    gives me...

    Code:
    [13:50:01.371] "{"_status_true":["load_option_tab_stones_1","load_option_tab_stones_1","load_option_tab_stones_2","load_option_tab_stones_2","load_option_tab_pearls_1","load_option_tab_pearls_1","load_option_tabs_costs_1","load_option_tab_edit_1","load_option_tab_edit_2"],"_status_false":["load_option_tab_pearls_2","load_option_tabs_costs_2"]}"
    [13:50:01.372] undefined
    If I force JSON to break by adding a print_r to the PHP fi_status_check function ... the JSON breaks as expected and alert(i); in my get_json() call gives me this ...

    Code:
    Array
    (
        [_status_true] => Array
            (
                [0] => load_option_tab_stones_1
                [1] => load_option_tab_stones_1
                [2] => load_option_tab_stones_2
                [3] => load_option_tab_stones_2
                [4] => load_option_tab_pearls_1
                [5] => load_option_tab_pearls_1
                [6] => load_option_tabs_costs_1
                [7] => load_option_tab_edit_1
                [8] => load_option_tab_edit_2
            )
    
        [_status_false] => Array
            (
                [0] => load_option_tab_pearls_2
                [1] => load_option_tabs_costs_2
            )
    
    )
    "{\"_status_true\":[\"load_option_tab_stones_1\",\"load_option_tab_stones_1\",\"load_option_tab_stones_2\",\"load_option_tab_stones_2\",\"load_option_tab_pearls_1\",\"load_option_tab_pearls_1\",\"load_option_tabs_costs_1\",\"load_option_tab_edit_1\",\"load_option_tab_edit_2\"],\"_status_false\":[\"load_option_tab_pearls_2\",\"load_option_tabs_costs_2\"]}"
    which all looks right to me ???
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,473
    Thanks
    13
    Thanked 361 Times in 357 Posts
    Quote Originally Posted by firepages View Post
    calling
    Code:
    console.log(v);
    console.log(v._status_true);
    gives me...
    Code:
    [13:50:01.371] "{"_status_true":["load_option_tab_stones_1","load_option_tab_stones_1","load_option_tab_stones_2","load_option_tab_stones_2","load_option_tab_pearls_1","load_option_tab_pearls_1","load_option_tabs_costs_1","load_option_tab_edit_1","load_option_tab_edit_2"],"_status_false":["load_option_tab_pearls_2","load_option_tabs_costs_2"]}"
    [13:50:01.372] undefined
    the first output is a String, not an object. so somehow you didn’t parse the JSON in JS.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    firepages (10-15-2013)

  • #6
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    Quote Originally Posted by Dormilich View Post
    the first output is a String, not an object. so somehow you didn’t parse the JSON in JS.

    ok, I see , just does not make any sense ... another function above uses the exact same call to get_json() which works... hmmm AND calls the status_check function.... not sure how that would make any difference though ??

    Code:
    function fi_update_val(oEvent,obj,table,item_no,callback){
    	if(status_flag==1){
    		alert('Record is sealed and can not be edited');
    		return;
    	}
    	var val=scrape_val(obj);
    	var str='?_f=fi_update_val&val_no={$vm_id}&'+obj.id+'='+val+'&table='+table;
    	if(item_no){
    		str+='&mixed='+item_no;
    	}
    	var v = get_json('val_main_loader.php'+str);
    	if(v){
    		msg_show(v.result);
    		if(v.callback){ 
    			window[v.callback](); //this works ?@#$!%
    		}
    		if(v._ready_state){
    			//NYI\DEPRACATED
    		}
    	}
    	if(callback){
    		window[callback]();
    	}
    	status_check();
    	return;
    }
    gonna try simply calling status_check onclick.... brb
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #7
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    AAAAAAGHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH (etc)

    sorry everyone... calling the class that produces the array differently than normal ... long story short...

    PHP Code:
    #produces a string...
    return json_encode($ax_status);

    #gives me the object and works as expected.
    echo json_encode($ax_status);exit; 
    I hate it when that happens.
    thanks for the inout everyone.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •