Jquery AJAX response from PHP has brackets around the JSON object
Results 1 to 5 of 5

Thread: Jquery AJAX response from PHP has brackets around the JSON object

  1. #1
    Senior Member
    Join Date
    Aug 2009
    Posts
    365

    Jquery AJAX response from PHP has brackets around the JSON object

    Here is my function:
    PHP Code:
    function get_it(){
    global 
    $mysqli;    
    if (
    $mysqli->connect_error) {
        die(
    'Connect Error (' $mysqli->connect_errno ') '
                
    $mysqli->connect_error);
    }


    $sql 'SELECT todo,date,name,id FROM task WHERE todo IS NOT NULL || todo > "" ' ;
    $sqlOrder 'Order by Date' ;
    $sql .= $sqlOrder ;

    $result $mysqli->query($sql);

    if(
    $result $mysqli->query($sql)) {
        
    $i0;
        while(
    $row $result->fetch_object()) {
        
     
    $function_result[$i]=     $row;
    $i++; 
    }    
    //end while
    header('Content-type: application/json');
    echo 
    json_encode($function_result);

    }
    //end if

    }//end function 
    The problem is when I don't send the header the JSON encoded string has an extra bracket around it. The header is supposed to tell Jquery that it should be expecting a JSON encoded object but sending the header doesn't get rid of the bracket. This is the header that is returned in the browser from the PHP script.
    http://localhost:8888/todo/todo1.html

    GET /todo/todo1.html HTTP/1.1
    Host: localhost:8888
    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:20.0) Gecko/20100101 Firefox/20.0
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language: en-US,en;q=0.5
    Accept-Encoding: gzip, deflate
    Connection: keep-alive
    If-Modified-Since: Thu, 02 May 2013 01:47:33 GMT
    If-None-Match: "c4a50a1-1915-4dbb26b051740"
    Cache-Control: max-age=0

    HTTP/1.1 304 Not Modified
    Date: Thu, 02 May 2013 01:54:00 GMT
    Server: Apache/2.2.23 (Unix) mod_ssl/2.2.23 OpenSSL/0.9.8r DAV/2 PHP/5.4.10
    Connection: Keep-Alive
    Keep-Alive: timeout=5, max=100
    Etag: "c4a50a1-1915-4dbb26b051740"
    ----------------------------------------------------------
    http://localhost:8888/todo/todo1.css

    GET /todo/todo1.css HTTP/1.1
    Host: localhost:8888
    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:20.0) Gecko/20100101 Firefox/20.0
    Accept: text/css,*/*;q=0.1
    Accept-Language: en-US,en;q=0.5
    Accept-Encoding: gzip, deflate
    Referer: http://localhost:8888/todo/todo1.html
    Connection: keep-alive
    If-Modified-Since: Tue, 19 Mar 2013 21:15:41 GMT
    If-None-Match: "c4d742b-1a5d-4d84d9b929940"
    Cache-Control: max-age=0

    HTTP/1.1 304 Not Modified
    Date: Thu, 02 May 2013 01:54:00 GMT
    Server: Apache/2.2.23 (Unix) mod_ssl/2.2.23 OpenSSL/0.9.8r DAV/2 PHP/5.4.10
    Connection: Keep-Alive
    Keep-Alive: timeout=5, max=99
    Etag: "c4d742b-1a5d-4d84d9b929940"
    ----------------------------------------------------------
    http://ajax.googleapis.com/ajax/libs.../jquery.min.js

    GET /ajax/libs/jquery/1.9.1/jquery.min.js HTTP/1.1
    Host: ajax.googleapis.com
    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:20.0) Gecko/20100101 Firefox/20.0
    Accept: */*
    Accept-Language: en-US,en;q=0.5
    Accept-Encoding: gzip, deflate
    Referer: http://localhost:8888/todo/todo1.html
    Connection: keep-alive
    If-Modified-Since: Fri, 08 Feb 2013 15:35:10 GMT
    Cache-Control: max-age=0

    HTTP/1.1 304 Not Modified
    Date: Wed, 01 May 2013 13:54:44 GMT
    Expires: Thu, 01 May 2014 13:54:44 GMT
    Age: 43156
    Server: GFE/2.0
    ----------------------------------------------------------
    http://ajax.googleapis.com/ajax/libs...uery-ui.min.js

    GET /ajax/libs/jqueryui/1.10.1/jquery-ui.min.js HTTP/1.1
    Host: ajax.googleapis.com
    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:20.0) Gecko/20100101 Firefox/20.0
    Accept: */*
    Accept-Language: en-US,en;q=0.5
    Accept-Encoding: gzip, deflate
    Referer: http://localhost:8888/todo/todo1.html
    Connection: keep-alive
    If-Modified-Since: Fri, 15 Feb 2013 22:11:00 GMT
    Cache-Control: max-age=0

    HTTP/1.1 304 Not Modified
    Date: Wed, 01 May 2013 13:55:41 GMT
    Expires: Thu, 01 May 2014 13:55:41 GMT
    Age: 43099
    Server: GFE/2.0
    ----------------------------------------------------------
    http://ajax.googleapis.com/ajax/libs.../jquery-ui.css

    GET /ajax/libs/jqueryui/1.10.0/themes/overcast/jquery-ui.css HTTP/1.1
    Host: ajax.googleapis.com
    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:20.0) Gecko/20100101 Firefox/20.0
    Accept: text/css,*/*;q=0.1
    Accept-Language: en-US,en;q=0.5
    Accept-Encoding: gzip, deflate
    Referer: http://localhost:8888/todo/todo1.html
    Connection: keep-alive
    If-Modified-Since: Thu, 24 Jan 2013 18:41:38 GMT
    Cache-Control: max-age=0

    HTTP/1.1 304 Not Modified
    Date: Wed, 01 May 2013 21:38:06 GMT
    Expires: Thu, 01 May 2014 21:38:06 GMT
    Age: 15354
    Server: GFE/2.0
    ----------------------------------------------------------
    http://ajax.aspnetcdn.com/ajax/moder...opment-only.js

    GET /ajax/modernizr/modernizr-2.0.6-development-only.js HTTP/1.1
    Host: ajax.aspnetcdn.com
    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:20.0) Gecko/20100101 Firefox/20.0
    Accept: */*
    Accept-Language: en-US,en;q=0.5
    Accept-Encoding: gzip, deflate
    Referer: http://localhost:8888/todo/todo1.html
    Connection: keep-alive
    If-Modified-Since: Mon, 20 Jun 2011 21:39:37 GMT
    If-None-Match: "8e7a8f8d922fcc1:0"
    Cache-Control: max-age=0

    HTTP/1.1 304 Not Modified
    Content-Type: application/x-javascript
    Age: 8324272
    Date: Thu, 02 May 2013 01:54:00 GMT
    Expires: Sat, 25 Jan 2014 17:36:08 GMT
    Connection: keep-alive
    ----------------------------------------------------------
    http://localhost:8888/todo/form_handler1.php

    GET /todo/form_handler1.php HTTP/1.1
    Host: localhost:8888
    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:20.0) Gecko/20100101 Firefox/20.0
    Accept: application/json, text/javascript, */*; q=0.01
    Accept-Language: en-US,en;q=0.5
    Accept-Encoding: gzip, deflate
    X-Requested-With: XMLHttpRequest
    Referer: http://localhost:8888/todo/todo1.html
    Connection: keep-alive
    Cache-Control: max-age=0

    HTTP/1.1 200 OK
    Date: Thu, 02 May 2013 01:54:01 GMT
    Server: Apache/2.2.23 (Unix) mod_ssl/2.2.23 OpenSSL/0.9.8r DAV/2 PHP/5.4.10
    X-Powered-By: PHP/5.4.10
    Content-Length: 2788
    Keep-Alive: timeout=5, max=98
    Connection: Keep-Alive
    Content-Type: application/json
    ----------------------------------------------------------
    http://ajax.googleapis.com/ajax/libs...eeee_1x400.png

    GET /ajax/libs/jqueryui/1.10.0/themes/overcast/images/ui-bg_glass_60_eeeeee_1x400.png HTTP/1.1
    Host: ajax.googleapis.com
    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:20.0) Gecko/20100101 Firefox/20.0
    Accept: image/png,image/*;q=0.8,*/*;q=0.5
    Accept-Language: en-US,en;q=0.5
    Accept-Encoding: gzip, deflate
    Referer: http://ajax.googleapis.com/ajax/libs.../jquery-ui.css
    Connection: keep-alive
    If-Modified-Since: Thu, 24 Jan 2013 18:41:38 GMT
    Cache-Control: max-age=0

    HTTP/1.1 304 Not Modified
    Content-Type: image/png
    Date: Thu, 02 May 2013 01:54:01 GMT
    Expires: Fri, 02 May 2014 01:54:01 GMT
    Cache-Control: public, max-age=31536000
    x-content-type-options: nosniff
    Server: sffe
    x-xss-protection: 1; mode=block
    ----------------------------------------------------------

    Do you know how to modify MAMP setting so I can get JSON encoded data?
    I also tried JSON.stringify(data); to get rid of the brackets and that didn't work.
    Thanks,

  2. #2
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,865
    HTTP/1.1 304 Not Modified
    Date: Thu, 02 May 2013 01:54:00 GMT
    Server: Apache/2.2.23 (Unix) mod_ssl/2.2.23 OpenSSL/0.9.8r DAV/2 PHP/5.4.10
    Connection: Keep-Alive
    Keep-Alive: timeout=5, max=100
    Etag: "c4a50a1-1915-4dbb26b051740"

    Of more help would be an example of the JSON. All any of that tells is is that you're making a lot of requests for things (mostly from CDNs) that haven't changed since the last time you requested them (except for the one that is sending back JSON data).

    The header is supposed to tell Jquery that it should be expecting a JSON encoded object but sending the header doesn't get rid of the bracket.
    It's not the responsibility of http headers to modify the content of the response.

    The problem is when I don't send the header the JSON encoded string has an extra bracket around it
    So the extra bracket is there when the header is not present and the extra bracket is there when the header is present. Maybe it's not the header that's the problem.
    Last edited by Weedpacket; 05-02-2013 at 12:30 AM.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  3. #3
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    Quote Originally Posted by jrough View Post
    The header is supposed to tell Jquery that it should be expecting a JSON encoded object
    You actually can (and should) specify how jQuery should treat the returned data:
    Code:
    $.ajax( {dataType:"json"} );

  4. #4
    Senior Member
    Join Date
    Aug 2009
    Posts
    365
    Quote Originally Posted by Weedpacket View Post
    Of more help would be an example of the JSON. All any of that tells is is that you're making a lot of requests for things (mostly from CDNs) that haven't changed since the last time you requested them (except for the one that is sending back JSON data).

    It's not the responsibility of http headers to modify the content of the response.

    So the extra bracket is there when the header is not present and the extra bracket is there when the header is present. Maybe it's not the header that's the problem.
    Well I do have the right json datatype in the AJAX. Below is the top of the jquery AJAX call. When I take out the line of the contentType it takes out the brackets but it leaves the word Object. When I leave the contentType in, I have the brackets.
    Code:
    .ajax({
        url: 'form_handler1.php',
    
    contentType:"application/json; charset=utf-8", 
    	dataType : "json",
    [{"todo":"","date":"0000-00-00","name":"","id":"609"},{"todo":"","date":"0000-00-00","name":"","id":"608"},{"todo":"","date":"0000-00-00","name":"","id":"607"},{"todo":"ddd","date":"0000-00-00","name":"","id":"606"},{"todo":"ddd","date":"0000-00-00","name":"","id":"605"},{"todo":"ddd","date":"0000-00-00","name":"","id":"604"},{"todo":"","date":"0000-00-00","name":"","id":"603"},{"todo":"","date":"0000-00-00","name":"","id":"602"},{"todo":"","date":"0000-00-00","name":"","id":"601"},{"todo":"laundry","date":"0000-00-00","name":"janis rough","id":"600"},{"todo":"janis rough","date":"0000-00-00","name":"","id":"640"},{"todo":"janis rough","date":"0000-00-00","name":"","id":"639"},{"todo":"","date":"0000-00-00","name":"","id":"638"},{"todo":"janis rough","date":"0000-00-00","name":"","id":"637"},{"todo":"janis rough","date":"0000-00-00","name":"","id":"636"},{"todo":"janis rough","date":"0000-00-00","name":"","id":"635"},{"todo":"janis rough","date":"0000-00-00","name":"","id":"634"},{"todo":"bark","date":"0000-00-00","name":"Junior","id":"629"},{"todo":"bark","date":"0000-00-00","name":"Junior","id":"628"},{"todo":"car wash","date":"0000-00-00","name":"Janis Rough","id":"624"},{"todo":"","date":"0000-00-00","name":"","id":"623"},{"todo":"","date":"0000-00-00","name":"","id":"621"},{"todo":"","date":"0000-00-00","name":"","id":"622"},{"todo":"","date":"0000-00-00","name":"","id":"619"},{"todo":"","date":"0000-00-00","name":"","id":"620"},{"todo":"","dat2013-01-04","name":"Janis Rough","id":"625"},{"todo":"do laundry","date":"2013-02-14","name":"janis rough","id":"633"},{"todo":"do laundry","date":"2013-02-14","name":"janis rough","id":"632"},{"todo":"do laundry","date":"2013-02-14","name":"janis rough","id":"631"},{"todo":"do laundry","date":"2013-02-14","name":"janis rough","id":"630"}]
    OR
    {"todo":"","date":"0000-00-00","name":"","id":"641"},{"todo":"","date":"0000-00-00","name":"","id":"642"},{"todo":"","date":"0000-00-00","name":"","id":"643"},{"todo":"","date":"0000-00-00","name":"","id":"644"},{"todo":"","date":"0000-00-00","name":"","id":"645"},{"todo":"","date":"0000-00-00","name":"","id":"646"},{"todo":"shopping","date":"2013-01-04","name":"Janis Rough","id":"627"},{"todo":"shopping","date":"2013-01-04","name":"Janis Rough","id":"626"},{"todo":"car wash","date":"2013-01-04","name":"Janis Rough","id":"625"},{"todo":"do laundry","date":"2013-02-14","name":"janis rough","id":"633"},{"todo":"do laundry","date":"2013-02-14","name":"janis rough","id":"632"},{"todo":"do laundry","date":"2013-02-14","name":"janis rough","id":"631"},{"todo":"do laundry","date":"2013-02-14","name":"janis rough","id":"630"}]

    Maybe these are actually the same, so it appears, but in the console the previous output from php has the word "object" added to it after sent to the browser. The output above is from hitting the php page after I hit the jquery page. So the firebug console has the word "object". I don't know why that would should up in the console but not on the php page but in any case neither one gets it right. Below is the beginning of the output from firebug console. Is that how firebug is interpreting the string? In any case I simply need to get rid of the brackets and the AJAX call should work. It worked before I don't know how the brackets got in there. I removed some of the lines from the center so the output wouldn't be so long.

    thanks,

    GET http://localhost:8888/todo/form_handler1.php

    200 OK
    484ms
    jquery.min.js (line 5

    [Object { date=

    "0000-00-00"

    , id=

    "609"

    , todo=

    ""

    , more...}, Object { date=

    "0000-00-00"

    , id=

    "608"

    , todo=

    ""

    , more...}, Object { date=

    "0000-00-00"

    , id=

    "607"

    , todo=

    ""

    , more...}, Object { todo=

    "ddd"

    , date=

    "0000-00-00"

    , id=

    "606"

    }, Object { todo=

    "ddd"

    , date=

    "0000-00-00"

    , id=

    "605"

    }, Object { todo=

    "ddd"

    , date=

    "0000-00-00"

    , id=

    "604"

    }, Object { date=

    "0000-00-00"

    , id=

    "603"

    , todo=

    ""

    , more...}, Object { date=

    "0000-00-00"

    , id=

    "602"

    , todo=

    ""

    , more...}, Object { date=

    "0000-00-00"

    , id=

    "601"

    , todo=

    ""

    , more...}, Object { todo=

    "laundry"

    , date=

    "0000-00-00"

    , name=

    "janis rough"

    , more...}, Object { todo=

    "janis rough"

    , date=

    "0000-00-00"

    , id=

    "640"

    }, Object { todo=

    "janis rough"

    , date=

    "0000-00-00"

    , id=

    "639"

    }, Object { date=

    "0000-00-00"

    , id=

    "638"

    , todo=

    ""

    , more...}, Object { todo=

    "janis rough"

    , date=

    "0000-00-00"

    , id=

    "637"

    }, Object { todo=

    "janis rough"

    , date=

    "0000-00-00"

    , id=

    "636"

    }, Object { todo=

    "janis rough"

    , date=

    "0000-00-00"

    , id=

    "635"

    }, Object { todo=

    "janis rough"

    , date=

    "0000-00-00"

    , id=

    "634"

    "

    , id=

    "614"

    , todo=

    ""

    , more...}, Object { date=



    "do laundry"

    , date=

    "2013-02-14"

    , name=

    "janis rough"

    , more...}]

    todo1.html (line 121)

  5. #5
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,865
    The brackets "[...]" are supposed to be there: you're encoding an array, and that's how arrays are represented in JSON.

    The word "Object" comes from Firebug successfully identifying the parsed object as a JavaScript object (and the "more..." is Firebug making it clear that it's not displaying every property or array element).

    PS: don't you think your post would have been easier to read if you'd used [code]...[/code] tags more judiciously?
    Last edited by Weedpacket; 05-02-2013 at 10:52 PM.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

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
  •