2

I don't know why this double json response are not successful:

[{"first_content":"content",...}][{"second_content":"content",...}]     

So i am getting the message Oops! Try Again.

if(isset($_GET['start'])) {
    echo get_posts($db, $_GET['start'], $_GET['desiredPosts']);
    echo get_posts1($db, $_GET['start'], $_GET['desiredPosts']);
    $_SESSION['posts_start']+= $_GET['desiredPosts'];
    die();
}


var start = <?php echo $_SESSION['posts_start']; ?>;
var desiredPosts = <?php echo $number_of_posts; ?>;
var loadMore = $('#load-more');

loadMore.click(function () {
                loadMore.addClass('activate').text('Loading...');
                $.ajax({
                    url: 'profile.php',
                    data: {
                        'start': start,
                        'desiredPosts': desiredPosts
                    },
                    type: 'get',
                    dataType: 'json',
                    cache: false,
                    success: function (responseJSON, responseJSON1) {
                        alert(responseJSON);
                        loadMore.text('Load More');
                        start += desiredPosts;
                        postHandler(responseJSON, responseJSON1);
                    },
                    error: function () {
                        loadMore.text('Oops! Try Again.');
                    },
                    complete: function () {
                        loadMore.removeClass('activate');
                    }
                });
            });

What is the solution to get a double json response ? With one there is no problem

1 Answer 1

6

"Double JSON response" as you call them is basically invalid JSON. You should have something like so:

{"first_content":"content", "second_content":"content",...}

Or as a couple of people mentioned:

[{"first_content":"content",...}, {"second_content":"content",...}]

You probably need to modify some server side code, your get_posts function could return a PHP array instead of a JSON array. Example:

function get_posts(){
    $array = array('content' => 'foo', 'title' => 'bar');
    return $array;
}

Then in your profile.php:

if(isset($_GET['start'])) {
    $posts = get_posts($db, $_GET['start'], $_GET['desiredPosts']);
    $posts1 = get_posts1($db, $_GET['start'], $_GET['desiredPosts']);
    echo array($posts, $posts1);
    $_SESSION['posts_start']+= $_GET['desiredPosts'];
    die();
}
Sign up to request clarification or add additional context in comments.

7 Comments

or wrap the arrays in a object
rather should be [{"first_content":"content",...},{"second_content":"content",...}]
The json content are from two independently queries.
Are you the one generating the JSON with a server side code? Could we see the code?
Ohh I see what you're doing, you're using get_posts to output a JSON array. Personally, I'd make get_posts generate PHP arrays, then merge the 2 of them with array_merge and then use json_encode on the merged array.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.