www.webdeveloper.com
Results 1 to 7 of 7

Thread: jQuery/PHP can't decode json data

  1. #1
    Join Date
    Apr 2010
    Location
    Brooklyn NY
    Posts
    104

    jQuery/PHP can't decode json data

    Pretty much I feel like I've tried everything. the data is write and sends out {"beans":1337,"uid","five"}, but I can't get the php code working. I'm trying to have the alert(data.response) return 1337, but right now I just get a blank alert statement

    HTML Code:
    var data = {};
    data.beans = 1337;
    data.uid = 'five';
    var dataString = JSON.stringify(data);
    
    $.ajax({
    	type:'GET',
    	url:'http://mvmdesign.org/other/Beacon/ws/user_data.php',
    	dataType:'jsonp',
    	data:{data:dataString},
    	contentType:'application/json',
    	success: function(data) {
    		alert(data.response);
    	}
    });
    I've also tried:
    HTML Code:
    $.getJSON('http://mvmdesign.org/other/Beacon/ws/user_data.php?data='+dataString+'&callback=?',function(data) {
    	alert(data.response);
    });
    PHP Code:
    $res json_decode($_GET['data'],true);
    $beans $res['beans'];
    echo 
    $_GET['callback'] . '({"response" : "' $beans '"});'

  2. #2
    Join Date
    Dec 2005
    Posts
    2,984
    EDIT: Bad first response....your PHP code is the problem.

    You don't need to use json_decode() on the $_GET superglobal - they're all separate parameters anyway.

    PHP Code:
    $beans $_GET['beans'];
    echo 
    $_GET['callback'] . '({"response" : "' $beans '"});'
    Last edited by aj_nsc; 06-24-2011 at 06:49 PM.
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  3. #3
    Join Date
    Apr 2010
    Location
    Brooklyn NY
    Posts
    104
    That doesn't work

    HTML Code:
    $.getJSON('http://mvmdesign.org/other/Beacon/ws/user_data.php?data='+dataString+'&callback=?',function(data) {
    	alert(data.response);
    });
    with:

    PHP Code:
    echo $_GET['callback'] . '({"response" : "' $_GET['data'] . '"});'
    alerts {"beans":1337,"uid":6}

    I just can't figure out how to extract beans for example out of $_GET['data']
    json_decode doesn't work, nor does $_GET['data']->{"beans"}

    It's not adding up for me

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,926
    It might be useful to dump what your getting to the PHP error log to find out exactly what is in there and its structure, e.g.:
    PHP Code:
    $res json_decode($_GET['data'], true);
    error_log("GET data:\n" print_r($restrue])); 
    Then check your PHP error log to find out what exactly was received.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Mar 2007
    Location
    Canada
    Posts
    99
    yes, also you could use Firebug to see what is the response.
    it might be just a simple notice that can break the JS

  6. #6
    Join Date
    Apr 2010
    Location
    Brooklyn NY
    Posts
    104
    found the problem

    PHP Code:
    $res json_decode(stripslashes($_GET['data']),true);
    echo 
    $res['uid']; 
    PHP was interpreting the the data as

    HTML Code:
    {\"beans\":1337,\"uid\":5}
    So just the stripslashes method was missing

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,926
    Sounds as if your PHP config has the now-deprecated magic_quotes_gpc "feature" turned on. You might want to consider either turning it off if you can, or else negating it's effects before using any get, post, or cookie data.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles