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 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2009
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts

    AJAX/json vars return undefined.

    Quite well versed in PHP, newer to AJAX/json/jQuery.

    Primary Page

    <html>
    <head>
    <script type="text/javascript" src="../resources/jQuery/jquery-1.7.2.min.js"></script>
    </head>
    <body>

    <div id="output">Change me</div>
    <?
    $data = array("task" => "getuniqueid" ,"id" => "3");
    ?>
    <script id="source" language="javascript" type="text/javascript">

    $(function ()
    {
    $.ajax({
    url: '../resources/ajax.api.php',
    data: <? echo json_encode($data); ?>,
    dataType: 'json',
    success: function(data)
    {
    var task = data[0];
    var id = data[1];

    //Change div
    $('#output').html("<b>Task:</b>"+task+"<b> ID: </b>"+id);
    }
    });
    });

    </script>
    </body>
    </html>

    ajax.api.php
    <?php
    chdir('..');
    include('includes/global.php');
    if($_GET['task']==getuniqueid)
    {
    $player = new player();
    echo json_encode($player->getuniqueID($_GET['id']));
    }
    ?>

    Output:


    Task:undefined ID: undefined

    If I manually visit the page with the parameters passed in the URL,I do get the proper json output.

    I have also tried setting the data toUnsuccessful)
    "task=getuniqueid&id=3"

    Any & all help is appreciated.

    Thank you.

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,371
    Thanks
    11
    Thanked 591 Times in 572 Posts
    your object has keys, not indices:
    Code:
    var task = data.task;
    var id = data.id;
    and you need to use $_POST ($_FORM?) not $_GET in php...

    if you like GET, just build the url by hand instead of using jQuery's data tool...
    Last edited by rnd me; 07-18-2012 at 06:20 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • Users who have thanked rnd me for this post:

    absoleet (07-18-2012)

  • #3
    New Coder
    Join Date
    Nov 2009
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Great, thanks for the response! I had been programming for hours by this point, can't believe it was due to not using the index..

    in regards to building the URL by hand, I can drop the data, and pass the params through the URL directly!?

    ex:

    $.ajax({
    url: '../resources/ajax.api.php?task=blah&id=1',
    dataType: 'json',
    success: function(data)
    {
    var task = data.task;
    var id = data.id;

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,371
    Thanks
    11
    Thanked 591 Times in 572 Posts
    here is one way:

    Code:
    function ob2url(ob){
      var buffer=[], param, enc=encodeURIComponent;
      for(param in ob){if(ob.hasOwnProperty(param)&& !ob[param].call){
        buffer[buffer.length]= enc(param)+"="+enc(ob[param]);
      }}//next own
     return buffer.join("&");
    }
    
    
    
    //myData= <? echo json_encode($data); ?> ;
    myData={"task": "getuniqueid", "id": 3}; //for demo
    
    $.ajax({
    url: '../resources/ajax.api.php?'+ob2url(myData),
    success: function(data)
    {
    var task = data.task;
    var id = data.id; 
    
    }
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%


  •  

    Posting Permissions

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