0

Here is a solution I'm using - The issue is explained below.

I don't understand why .getJSON() is failing. I am compensating by using .post() which comes back as a String instead of the JSON.

$.post('test.php', { action: 'test' }, function(data, status){

    console.log(jQuery.parseJSON(data)); //form the JSON from a string after the POST

});

It works fine for now but puts more stress on the browser and end-user. It might actually be better this way for large-scale deployment to save a little bit of server CPU? Anyways. Strange little bug.



I used this previous answer to get started with what I'm doing: using jquery $.ajax to call a PHP function

and double checked my basic usage against: http://api.jquery.com/jQuery.getJSON/ and http://api.jquery.com/jQuery.post/

I have complete success with $.post but when using $.getJSON there isn't even a status returned ..

For clarification: I'm editing these functions to ask the question. I'm testing with usable json data that I've already been handling I'm just adding the ability to call php functions through ajax requests. Don't be thrown off with my strange php echo json_encode. My working data is validated.

Anyways, here is the .js:

$.post('test.php', { action:'test' }, function(data, status){
    console.log(data); //{"test_result":"success"}
    console.log(status); //success
});

$.getJSON('test.php', { action:'test' }, function(data, status){
    console.log(data); //nothing
    console.log(status); //nothing
}).fail(function( jqxhr, textStatus, error ) {
    console.log(textStatus); //parsererror
    console.log(error); //SyntaxError: Unexpected end of input
});

and my .php:

<?php
    try {
        if(isset($_POST['action']) && !empty($_POST['action'])) {
            $action = $_POST['action'];
            switch($action) {
                case 'test':
                    echo json_encode(array('test_result' => 'success'));
                    break;
            }
        }
    } catch(Exception $e) {
            echo "ERROR: " . $e->getMessage();
    }
?>

This successfully executes as a $.post but when I switch to $.getJSON it no longer functions. Is there a simple reason why?

Community
  • 1
  • 1
Qwiso
  • 11
  • 4
  • I have found many posts saying it fails because the data isn't valid JSON but I am checking my data in browser and then copy/paste to jsonlint.com to validate and it says it's fine. I'm really lost. – Qwiso Dec 15 '13 at 06:03

1 Answers1

0

Your test.php file are generating bad JSON format, and getJSON fails, I suggest you:

echo json_encode(array('test_result' => 'success'));

With this code you will get: {"test_result":"success"}

With your original code echo json_encode("[{'test_result':'success'}]"); you will get "[{'test_result':'success'}]"

Try adding fail callback like as:

$.getJSON('test.php', { action:'test' }, function(data, status){
    console.log(data); //doesn't fire
    console.log(status); //doesn't fire
}).fail(function( jqxhr, textStatus, error ) {
    var err = textStatus + ", " + error;
    console.log( "Request Failed: " + err );
});
Ignacio Ocampo
  • 2,693
  • 1
  • 20
  • 31
  • I was just editing to make that more fool-proof. `$test = array("test"=>"success"); echo json_encode($test);` still fails. As does your suggestion. The issue is that `$.getJSON` is never executing and I suspect the `{ action: 'test' }` has something to do with it. – Qwiso Dec 15 '13 at 05:51
  • I update my answer, I suggest use **fail** function to debug and catch errors. – Ignacio Ocampo Dec 15 '13 at 05:58
  • That's quite useful. Yes: `parsererror, SyntaxError: Unexpected end of input` – Qwiso Dec 15 '13 at 05:59
  • I will see if this leads me to a solution. Thank you. – Qwiso Dec 15 '13 at 06:00