Reputation: 5802
I have a JSON call without callback. I want to parse data.
$("button").click(function(){
$.getJSON(
"url",
function(result){
$.each(result, function(i, field){
$("div").append(field + " ");
});
});
});
and HTML
<button>Get JSON data</button>
<div></div>
But there is not any fetched data. What is wrong in my code?
Upvotes: 0
Views: 355
Reputation: 100195
A suggestion, since the site that you are trying to access doesnot seem to support JSONP as well, you could try to get the contents from some server side language, say PHP, like
<?php
echo file_get_contents(url);
?>
And use jquery getJson,
$(document).ready(function(){
$.getJSON('your_server-side_page.php', function(data){
$.each(data, function(i, entry) {
// Process your data here
});
});
});
Hope that helps
Upvotes: 3
Reputation: 93
I agree with Sarfraz. It is because it's a remote based request. One way that I got around this problem in the past was by using a server side file (Such as a .php or .aspx) to load the URL and then performing the getJSON on that locally accessible file.
This is because your server side files can connect to the remote host.
For example you could create a sample test.php file that jquery accesses with this code:
<?php
header('Content-type: application/xml');
//Get the remote content and output it for Jquery's Local use
echo file_get_contents(url);
?>
Then just call (relative of course):
$("button").click(function(){
$.getJSON(
"/test.php",
function(result){
$.each(result, function(i, field){
$("div").append(field + " ");
});
});
});
Using things like JsonP is good but sometimes they are overkill. I hope this helps!
Upvotes: 2
Reputation: 74086
From the looks of it, you are running into a case of the same origin policy. This basically states, that you can't use data retrieved from other hosts on your page.
As I can't find an (english) API documentation for the service, I recommend you check the API for JSONP and try to use that functionality (if present).
Another option would be to write a script on your server, which acts as a proxy for the data: Retrieving the request from your page, forwarding the request to http://www.bigpara.com
, getting the results from http://www.bigpara.com
and passing those results to your page.
Upvotes: 1