Вопрос

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);
        })
    });
});
Это было полезно?

Решение 2

The problem was a cross-domain problem.

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

Другие советы

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);
        })
    });
});
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top