0

Json file:

{
"weather": [
    {
        "location": "G",
        "temp": "9"
    },
    {
        "location": "L",
        "temp": "6"
    },
    {
        "location": "W",
        "temp": "10"
    }
]

}

Script

<script type="text/javascript">
                    // <![CDATA[
                    $(document).ready(function() {
                        var url =  ".../Json.php";
                        $.getJSON(url + "?callback=?", null, function(weather) {
                            for(i in weather) {
                                location = weather[i].location;
                                temp = weather[i].temp;
                                $("#footer").append(location.text + temp.text+"<hr />");
                            }
                        });
                    });

                    // ]]>
</script>


Can somebody show me where I did incorrectly? Is that because the json url I used belongs to different domain ? I tried using jsonp but it still doesn't work,....
Thank you very much.
P/S: Where I got the Jsonp idea Example and I can't configure the server, it's forbidden.

Community
  • 1
  • 1
Xitrum
  • 7,765
  • 26
  • 90
  • 126

2 Answers2

1

The problem is that you are requestion JSONP and getting JSON. It will just be parsed and silently ignored as there is no function call to do anything with the object.

Your JSONP response should have a function call around the object, like this:

func({
  "weather": [
    {
        "location": "G",
        "temp": "9"
    },
    {
        "location": "L",
        "temp": "6"
    },
    {
        "location": "W",
        "temp": "10"
    }
  ]
});

Use the value of the querystring parameter callback as the function name instead of func in the example above.


Then when you get the result, it's not an array, it's an object that has a property that is an array. Use the weather property to get the array:

for(i in weather.weather)
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • what do u mean by "Use the value of the querystring parameter callback as the function name instead of func in the example above."? – Xitrum Oct 02 '12 at 17:40
  • @Aptos: jQuery will send a parameter like for example `callback=callback67263487264`, then you should use that name in the JSONP response; `callback67263487264({"weather":[...]});` – Guffa Oct 02 '12 at 18:56
  • hmmmm, I'm new to Ajax and JS thus can you please explain how can I get that number? I wrote an php script to print out the json document.. – Xitrum Oct 02 '12 at 20:34
  • @Aptos: The name for the function is send in the query string to the server, so you would need to pick that up in the PHP script and use it to print out the JSONP document. – Guffa Oct 02 '12 at 20:39
  • I'm sorry but I really can't get that idea :(( – Xitrum Oct 02 '12 at 20:47
  • @Aptos: jQuery sends a request with an URL in the form `Json.php?callback=SOMETHINGHERE`, and your PHP script has to return a JSONP result in the form `SOMETHINGHERE({...})`. – Guffa Oct 02 '12 at 21:26
  • noo, my php just echo json line... and how can I track all of that? is there any tool? – Xitrum Oct 02 '12 at 22:06
  • @Aptos: Yes, that's what's missing, the PHP should pick up the callback name and echo that out as a call around the JSON string to make it a JSONP string. What is it that you want to track? – Guffa Oct 02 '12 at 22:29
  • yes, so how can i "PHP should pick up the callback name and echo that out "? to find the correct callback=? ... – Xitrum Oct 02 '12 at 22:39
  • @Aptos: Use ``. See http://php.net/manual/en/reserved.variables.get.php – Guffa Oct 02 '12 at 22:49
  • @Aptos I've you're still in doubt, check this link: http://www.geekality.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp/ – AardVark71 Oct 03 '12 at 08:14
0

You cannot use this

var url =  ".../Json.php";

You cannot make call to other domain url

In spite of the power of the XMLHttpRequest API, its usage is limited by the “same-origin” policy. What this means is that the hostname of the url you are sending the XMLHttpRequest cannot be different from the hostname of the web server.

StaticVariable
  • 5,253
  • 4
  • 23
  • 45