1

All of this worked before, I haven't changed anything and now it doesn't. I have two elements named with id="live_title" and id="live_game". AJAX purpose is to update them.

My jQuery:

var tytul = function getTitle(){
    $.ajax({
        type : 'GET',
        dataType: 'json',
        url : 'ajax/getTitle.php',
        success : function(data) {
            for (var prop in data) {
                $('#live_'+prop).html(data[prop]);
            }
        }
    });
}
setInterval( tytul, 10000 );

AJAX response as seen in Firebug:

{"title":"Some title title...","game":"Name of the game"}

PHP Code sending stuff:

header('Content-Type: application/json');
// creating assoc array here
echo json_encode(array( 'title' => $title, 'game' => $game));

JavaScript console is empty.

When I visit ajax/getTitle.php in my browser, there is no error and displayed is the same thing as normally in Firebug. Response gives correct header (it's "Content-Type: application/json").

I've added

error : function(data) {
    alert(data);
}

To my .ajax() function. It pops [object Object]. for alert(data.title) or alert(data['title']) it's undefined

Sumurai8
  • 20,333
  • 11
  • 66
  • 100
Forien
  • 2,712
  • 2
  • 13
  • 30
  • Are you setting the header to json in the php file? – Andrew Jun 08 '14 at 21:57
  • what do you get if you put the URL in a browser ? – Francisco Corrales Morales Jun 08 '14 at 21:58
  • 1
    What is the response code of the PHP script? The success handler is not called if it is an error code, even if the output is valid json. – GolezTrol Jun 08 '14 at 21:58
  • 3
    Remove `var data = $.parseJSON(data);`, jQuery parses the JSON for you (`dataType: 'json'`). Calling `$.parseJSON` on an object does not work. – Felix Kling Jun 08 '14 at 21:59
  • As always, when your success handler isn't being called, install an error handler and see if it's being called and what the arguments to it are. – jfriend00 Jun 08 '14 at 22:00
  • Prepend a slash in url, like : `/ajax/getTitle.php` – Yang Jun 08 '14 at 22:00
  • @Scorpion: How can you give such an advice without more context information? – Felix Kling Jun 08 '14 at 22:00
  • I have header, and when I go to my php script it shows the same string as seen in Firebug. About error handle, how should it look? – Forien Jun 08 '14 at 22:01
  • @FelixKling He said it was working before, so I assumed that he moved that into a server – Yang Jun 08 '14 at 22:01
  • @Scorpion: It doesn't have any thing to do with server or local development though, only with page structure. If that changed then yes, the relative URL might be a problem. But it's nothing but a guess. – Felix Kling Jun 08 '14 at 22:04
  • @jfriend00 in code `error : function(data) { alert(data); }` it shows [object Object]. data.title gives "undefined". @Scorpion with "/ajax/getTitle.php" i get 404 error in console – Forien Jun 08 '14 at 22:07
  • 1
    @Forien: Have a look at documentation: https://api.jquery.com/jquery.ajax/. The argument passed to the `error` callback *is not* the response from the server. It's the `jqXHR` object with more information about the error. Hence `data.title` will of course return `undefined`. Use `console.log` instead of `alert`. – Felix Kling Jun 08 '14 at 22:36
  • @Forien - look at the 2nd and 3rd arguments to the error callback. The callback gets called with these args: `( jqXHR jqXHR, String textStatus, String errorThrown )` – jfriend00 Jun 08 '14 at 22:38
  • @FelixKling @jfriend00 second is `parseerror`, third is `SyntaxError: Unexpected token` – Forien Jun 08 '14 at 22:41
  • Is that the error you get as well when you remove the `var data = $.parseJSON(data);` part? If so, the response you receive from the server is not valid JSON. Maybe your PHP file contains a BOM which causes trouble. See http://stackoverflow.com/q/17427775/218196 – Felix Kling Jun 08 '14 at 22:42
  • @FelixKling OMG, yes! It's BOM. I've never assumed this can cause any problems and I always make my files UTF-8 with BOM. Is there any way to make json/AJAX work with BOM? Or I have to change all files to UTF-8 without BOM? And I still don't know why all this worked earlier (I didn't change charset or encoding) – Forien Jun 08 '14 at 22:50
  • Honestly I don't know. I think you have to save all the files without BOM. – Felix Kling Jun 08 '14 at 23:07
  • Can you do the `console.log(JSON.stringify(data));`? And tell me the results? – fsi Jun 08 '14 at 23:16

1 Answers1

3

Try to console your ajax response first and then step forward to further operations

var tytul = function getTitle(){
$.ajax({
    type : 'GET',
    dataType: 'json',
    url : 'ajax/getTitle.php',
    success : function(data) {
       //if successfull           
       console.log(data);

    },
    error:function(data){
    //if error
    console.log(data);
    }
  });
}
setInterval( tytul, 10000 );
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103