1

For some reason, the .done() function is not firing after a successful AJAX call. This is the Javascript file:

//Create groups
$("#compareschedulesgroups_div").on('click', '.compareschedules_groups', function() { //When <li> is clicked
    //Get friendids and friend names
    print_loadingicon("studentnames_taglist_loadingicon");
    $.ajax({
        type: "POST", //POST data
        url: "../secure/process_getgroupdata.php", //Secure schedule search PHP file
        data: { groupid: $(this).find(".compareschedules_groups_groupnum").val() }, //Send tags and group name
        dataType: "JSON" //Set datatype as JSON to send back params to AJAX function
    })
    .done(function(param) { //Param- variable returned by PHP file
        end_loadingicon("studentnames_taglist_loadingicon");
        //Do something
    }); 
});

Safari's Web Developement tab says that my external PHP file returned this, which is the correct data:

{"student_id":68,"student_name":"Jon Smith"}{"student_id":35,"student_name":"Jon Doe"}{"student_id":65,"student_name":"Jim Tim"}{"student_id":60,"student_name":"Jonny Smith"}

The PHP file looks like this. Basically, it gets a json_encoded value, echos a Content-Type header, then echos the json_encoded value.

    for ($i = 0; $i<count($somearray); $i++) {
        $jsonstring.=json_encode($somearray[$i]->getjsondata());
    }

    header("Content-Type: application/json", true);
    echo $jsonstring;

Edit: I have an array of objects- is this the way to go about json_encoding it?

The getjsondata function is this, from another Stack Overflow question:

    public function getjsondata() {
        $var = get_object_vars($this);
        foreach($var as &$value){
           if(is_object($value) && method_exists($value,'getjsondata')){
              $value = $value->getjsondata();
           }
        }
        return $var;
    }
2
  • that isn't valid json... show more of your php
    – Kevin B
    Oct 22, 2013 at 16:14
  • As a followup to Kevin's comment, check out jsonlint.com to validate your data. Oct 22, 2013 at 16:19

1 Answer 1

2

You are looping the json_encode() call, outputting individual objects. This is invalid JSON when they are concatenated together. The solution is to append each object to an array, then encode the final array. Change the PHP to:

$arr = array();

for ($i = 0; $i<count($somearray); $i++) {
    $arr[] = $somearray[$i]->getjsondata();
}

header("Content-Type: application/json", true);
echo json_encode($arr);

This will output a JSON representation of the array of objects for example:

[
    {
        "student_id": 68,
        "student_name": "Jon Smith"
    },
    {
        "student_id": 35,
        "student_name": "Jon Doe"
    },
    {
        "student_id": 65,
        "student_name": "Jim Tim"
    },
    {
        "student_id": 60,
        "student_name": "Jonny Smith"
    }
]
1
  • Oh wow that was dumb- thanks for the answer- i'll accept it in a few minutes Oct 22, 2013 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.