質問

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