1

I am calling a php script to get the data using ajax. It works without any problems on chrome and other browsers but on IE (of course) it does not work.

My data format is as follows:

[1234000000000,56]

this is the script I am trying to call to get the abobe value:

$.ajax({

        url: 'get_cpu.php', 
        success: function(data) {
          var myObj = JSON.parse(data); 
                        alert(obj);

I tried outputing this file in many different formats to make IE happpy without any luck. How can I format this data in json so that ie understands? I really appreciate any insight?

Best regards,

user1471980
  • 10,127
  • 48
  • 136
  • 235

2 Answers2

1

Old versions of IE don't have the built-in JSON object. Therefore, JSON.parse doesn't exist. It has nothing to do with your JSON format.

Since you are using jQuery, you don't need to worry JSON parsing; it will take care of it. In your $.ajax call, add dataType: 'json'. This will make jQuery parse it for you automatically.

$.ajax({
    url: 'get_cpu.php',
    dataType: 'json',
    success: function(data) {
        console.log(data);
    }
});

If you weren't using jQuery, you would have to use a JSON replacement library, like json3.js.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • @user1471980: *What* is undefined? I want to see the *entire* string. For example: `ReferenceError: xyz is not defined`. – gen_Eric Jun 14 '13 at 14:36
  • when I run this in chrome, I get this: [1371206178000, 55] , in IE I get this: LOG:undefined – user1471980 Jun 14 '13 at 14:37
  • @user1471980: Oh. Sorry. That's not an error, just the log's output. Is this what you get with the *exact* code in my answer (you did remove the `JSON.parse` line, and add `dataType: 'json'`, right)? – gen_Eric Jun 14 '13 at 14:38
  • I copied and pasted your code exactly. As you can see from chrome, it works, but not in IE – user1471980 Jun 14 '13 at 14:39
  • this is the entire ajax section: $.ajax({ url: 'get_cpu.php', dataType: 'json', success: function(data) { console.log(data); }, cache: false }); – user1471980 Jun 14 '13 at 14:40
  • I am using jquery.min.js, do you think I should use the full version for json? – user1471980 Jun 14 '13 at 14:44
  • @user1471980: That is the "full" version of jQuery, it's just "minified". What version of jQuery are you using? The 2.x series doesn't support old versions of IE, you should be using 1.9.x. – gen_Eric Jun 14 '13 at 14:45
  • @user1471980: I'm curious. Can you change the `success` function to `function(data, status, jqXHR){ console.log(jqXHR.responseText); }` and show me what gets printed? Because I tested this in IE8 and it worked for me. P.S. What version of IE are you using? They are all completely different. – gen_Eric Jun 14 '13 at 14:48
  • I get this: LOG: undefined – user1471980 Jun 14 '13 at 14:54
  • @user1471980: Hmm? I don't know what to tell ya. This is all working for me in IE8. As a last-ditch effort, try `$.getJSON('get_cpu.php', function(data){ console.log(data); });`/ – gen_Eric Jun 14 '13 at 14:57
  • @user1471980: Sorry. I got nothing else. – gen_Eric Jun 14 '13 at 15:01
  • what version of jquery are you using? – user1471980 Jun 14 '13 at 15:05
  • @user1471980: 1.9.1. What version are *you* using? – gen_Eric Jun 14 '13 at 15:06
  • I am using 1.8.2. I saw this post http://bugs.jquery.com/ticket/13169, do you think this might be it? – user1471980 Jun 14 '13 at 15:09
  • @user1471980: What version of IE are you using? What does IE's dev tools say for "Document Mode" and "Browser Mode"? – gen_Eric Jun 14 '13 at 15:15
  • I tried this with IE8 and iE9, the same issue. I get the Log:undefined error. it is crazy, dont know what to do. – user1471980 Jun 14 '13 at 15:23
  • do you think I have put something like this in my php script: header('Content-Type: application/json'); – user1471980 Jun 14 '13 at 16:03
  • @user1471980: You could try that :) – gen_Eric Jun 14 '13 at 16:04
0

Please refer to this page and wikipedia. All of code there runs in all browsers.

Saturnix
  • 10,130
  • 17
  • 64
  • 120
Anil Gupta
  • 632
  • 4
  • 16