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.

5
  • Are you aware that by passing callback=? you're probably requiring a jsonp response ? What's the server ? Doesn't it provide some doc or samples ? Commented Oct 2, 2012 at 17:31
  • yes, i need it because the url is located in a different domain ... Commented Oct 2, 2012 at 17:31
  • Have you tried to use console.log inside of your callback? Commented Oct 2, 2012 at 17:31
  • If the server is configured to accept cross domain requests, you don't need jsonp. If it isn't configured to answer in jsonp, it won't. Commented Oct 2, 2012 at 17:32
  • I can't change the configures of the server thus i think jsonp will do the job here... Commented Oct 2, 2012 at 17:36

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

11 Comments

what do u mean by "Use the value of the querystring parameter callback as the function name instead of func in the example above."?
@Aptos: jQuery will send a parameter like for example callback=callback67263487264, then you should use that name in the JSONP response; callback67263487264({"weather":[...]});
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..
@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.
I'm sorry but I really can't get that idea :((
|
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.

1 Comment

And if the server explicitly accepts cross-domain requests (with CORS headers) then, well, it accepts them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.