Fahd
Fahd

Reputation: 353

This jQuery page doesn't work, Why?

why doesn't this jQuery code work ? what is wrong ?

The expected behavior that it alerts out the content of the requested JSON file but it doesn't.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<head> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> 
<script type="text/javascript"> 
$.ajax({
  type: 'GET',
  url: 'http://api.xxxxxxxxx.com/0.1/cities/key=xxxx',
  dataType: 'jsonp',
  crossDomain: true,
  success: function(data){
              $.each(data, function(i, el){
                    alert(el.city)
               });
         ;}
});
</script> 

<title>Example</title> 
</head> 
<body></body> 
</html>

Upvotes: 1

Views: 152

Answers (2)

genesis
genesis

Reputation: 50976

function needs to have callback

callback({json:true});

and if you do not have control over target server, it's not possible to make jsonp request

Upvotes: 0

Paul Creasey
Paul Creasey

Reputation: 28844

The resource that you are loading is not JSONP, it is just a JSON string, JSONP should look like this:

callback("jsonString");

Not all json can be loaded as jsonp, it has to be setup for it specifically.

JSONP

Upvotes: 2

Related Questions