0

I have a js file. In this file I am trying to getjSON data from the serverside. The json which returns from the server side is

{"STATUS":"TRUE","DEVICE_KEY":"80147459","ERROR_MESSAGE":""}

The script below do not return any alert. What am I doing wrong? (I checked if jQuery and jQueryMobile is loaded. there is no problem)

The same script works on my local machine but doesn't work on the distance server. Can this be an apache or php setup problem?

thanks

$(window).load(function () {
    $.getJSON("http://.... myfile.php", function (data) {
        $.each(data, function (k, v) {
            alert("key: " + k + "  val:" + v);
        })
    });
});
Calipso
  • 957
  • 3
  • 15
  • 33
  • cross-domain ajax maybe – Su4p Apr 07 '14 at 12:03
  • It is possible that the $.getJSON function returned an error ? [Try this question][1]. [1]: http://stackoverflow.com/questions/1740218/error-handling-in-getjson-calls – blorkfish Apr 07 '14 at 12:06
  • can't u use a simple string ? a.g. key-value and split it afterwards :) – Dieter Apr 07 '14 at 12:13
  • the same php exists on my localhost. the script works on my local machine but doesn't work on the remote server. Is there a server setting that I am missing? – Calipso Apr 07 '14 at 12:13
  • Dieter this one was the most easies sample. I have more complicated samples :) the problem is, it doesn't work on the distance server at all. Do I have to change any php or apache setting? – Calipso Apr 07 '14 at 12:17

2 Answers2

1

I think you are not parsing it.

The JSON is just a Javascript Object Notation and it is a string. You'll have to parse it to convert it to a object.

Do this:

$(window).load(function () {
    $.getJSON("http://.... myfile.php", function (data) {
        data = JSON.parse(data); // add this line
        $.each(data, function (k, v) {
            alert("key: " + k + "  val:" + v);
        })
    });
});
Amit Joki
  • 58,320
  • 7
  • 77
  • 95
0

The problem was a cross-domain problem.

I used jsonp and it worked.. see http://json-p.org/

Calipso
  • 957
  • 3
  • 15
  • 33