문제

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