I have this url as json object which is provided by google API.
https://www.googleapis.com/customsearch/v1?key=AIzaSyAo6DqmlMti9ID7lL532A7-6Miu1QHcMqI&cx=013881670411723352605:b1y6-jfsiki&q=web%20developer?alt=json-in-script&callback=listEvents
I want to use javaScript and want to see complete json object.This is my script code , but it doesn't work. please help me , i am struggling from 3 days.
function listEvents(root){
$.getJSON('https://www.googleapis.com/customsearch/v1?key=AIzaSyAo6DqmlMti9ID7lL532A7-6Miu1QHcMqI&cx=013881670411723352605:b1y6-jfsiki&q=web%20developer?alt=json-in-script&callback=listEvents', function(data) {*/
alert(root)
});
}
function init() {
listEvents();
}
window.onload = init;
Thanks in advance
You are giving a callback option in url that is again a call to listEvents(). That is not correct. Use your own success callback. this will work:
function listEvents() {
$.getJSON('https://www.googleapis.com/customsearch/v1?key=AIzaSyAo6DqmlMti9ID7lL532A7-6Miu1QHcMqI&cx=013881670411723352605:b1y6-jfsiki&q=web%20developer?alt=json-in-script', function (data) {
console.log(data)
});
}
Demo example
The output is in JSONP format and not JSON
JSONP({JSON})
Jquery JSONP: http://learn.jquery.com/ajax/working-with-jsonp/
(function($) {
var url = 'https://www.googleapis.com/customsearch/v1?key=AIzaSyAo6DqmlMti9ID7lL532A7-6Miu1QHcMqI&cx=013881670411723352605:b1y6-jfsiki&q=web%20developer?alt=json-in-script&callback=listEvents';
$.ajax({
type: 'GET',
url: url,
async: false,
jsonpCallback: 'listEvents',
contentType: "application/json",
dataType: 'jsonp',
success: function(json) {
console.dir(json);
},
error: function(e) {
console.log(e.message);
}
});
})(jQuery);
BTW your Google Daily Limit Exceeded
Related
I'm trying to get data from external server using JSONP, but I've stuck on the callback function.
function processLinks(data) {
alert('IT WORKS!');
};
function getLinks() {
$.ajax({
type: 'GET',
url: 'https://external-site.com/test.cgi',
dataType: 'jsonp',
success: function (data) {
console.log(data)
}
});
};
When I call getLinks(), I get ReferenceError: processLinks is not defined.
External site returns processLinks({"mark": "dog", "number":"33"});.
Thank you for any help
I have this function, and every time I call it, I need the imagesUploadScript variable to be updated with a new URL. I've implemented in the server side a JSON response with the desired URL for each request, but I'm not able to get that JSON value with jQuery.
$(function () {
$('.editor').editorInsert({
editor: editor,
addons: {
images: {
imagesUploadScript: /* The url*/
}
});
});
I have tried this, but doesn't seem to work :S
$.getJSON("/admin/upload_url",function(result){
return JSON.stringify(result)
})
EDIT
I have restructured my code, this way my function accepts a callbacks as #Mohamad suggested and thanks to this question:
function getURL(callback) {
var url;
$.ajax({
type: "GET",
async: false,
url: "/admin/upload_url",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
url = data['url'];
callback(url);
} //success
});
}
But I'm not able to return the url for imagesUploadScript but yes this way
getURL(function(url){
console.log(url);
});
I'm confused, how should I declare this function inside the other one so imagesUploadScript get's a new URL every time is called ?
Thanks in advance for any help ! :D
How can I add a JSON file in jsfiddle? I have a JSON file but I am not able to attach it in jsfiddle. I can make a JSON object and use it, but is there any way to add an external JSON file to a fiddle?
Myjson.com provides api, which runs in Jsfiddle.net.
Custom my myjson:
// Loading JSON with CROS
var url = 'https://api.myjson.com/bins/3ko1q';
$.ajax({
type: 'GET',
url: url,
async: false,
contentType: "application/json",
dataType: 'json',
success: function (data) {
alert('success');
console.log(data);
},
error: function (e) {
alert('error');
console.log(e);
}
});
Myjson GET Example:
// 1. Create valid uri via POST
// 2. GET using newly created uri
var obj = {
"key": "value",
"key2": "value2"
};
var data = JSON.stringify(obj);
$("#clickMe").click(function () {
$.ajax({
url: "https://api.myjson.com/bins",
type: "POST",
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data, textStatus, jqXHR) {
// load created json
$.get(data.uri, function (data, textStatus, jqXHR) {
var json = JSON.stringify(data);
$("#data").val(json);
});
}
});
});
You can harness the power of Cross-Origin Resource Sharing (CORS) to achieve your task.
Basically how CORS works is that if the Access-Control-Allow-Orign header is set in the HTTP response, then the content loaded by AJAX can be used in our script regardless of the fact it is on the same domain or some other.
Now for your purpose, you can upload your local JSON file to Dropbox's Public folder and get a Public URL, that you can load by a simple AJAX call.
The AJAX call will succeed in this case because Dropbox sets the following value in the response Access-Control-Allow-Orign:* which means any domain can use the loaded content.
Jquery code will be something like this(you can even use pure JavaScript if you prefer ).
var url = 'https://dl.dropboxusercontent.com/u/94145612/example.json';
var myJsonData= {};
$.ajax({
type: 'GET',
url: url,
async: false,
contentType: "application/json",
dataType: 'json',
success: function (data) {
alert('success');
console.log(data);
myJsonData= data;
},
error: function (e) {
alert('error');
console.log(e);
}
});
Example JSFiddle
Based on your comment, you want to use a pure JSON file as an external resource in a jsFiddle. You can't do this, because pure JSON is not JavaScript. Say you try to include http://example.com/foo.json as an external resource, and that file contains the following:
{"foo":"bar"}
This will result in Uncaught SyntaxError: Unexpected token :, because the JSON object is not valid JavaScript by itself.
But if you assign the JSON object to a variable, like so:
var foo = {"foo":"bar"};
then no problem.
Solution: use a modified version of your file to initialize a variable for use in the jsFiddle.
I'm practicing with the PHP RestServer Class. But I can't get the correct data from it if I use an Ajax call on it. I have the following code:
<?php
require_once "locationOfRestServer.php";
class HelloWorld
{
public static function sayHello()
{
return array("Response" => "Hello World");
}
}
$rest = new RestServer('HelloWorld');
$rest->handle();
And in my javascript file I use the following:
this.helloWorld = function() {
$.ajax({
url: 'locationOfHelloWorld.php'
type: 'POST',
dataType: 'json',
success: function(data){
console.log(data);
}
});
};
I get the following error:
error: "No method was requested."
Because; whenever I use it, I have to go to localhost/HelloWorld.php?method=sayHello which is actually working.
So I added the following line to the ajax call:
method: 'sayHello',
But it still keeps giving me the same error.
Try this,
$.ajax({
url: 'locationOfHelloWorld.php'
type: 'GET',// use GET method according to your working url
data:{method: 'sayHello'},// use method in data parameter
dataType: 'json',
success: function(data){
console.log(data);
}
});
I have created a url that I need to pass to police data API.
var api_url="http://policeapi2.rkh.co.uk/api/locate-neighbourhood?q="+latlon;
document.write(api_url);
The URL works when you manually input it into a browser, but I need some JavaScript that sends the API request and spits it onto the page. My JavaScript is ok but I have no knowledge of jQuery.
Thank you in advance, please keep it simple brainy folk.
try the following.. its is working
JS Fiddle http://jsfiddle.net/9AZyZ/
function abc(latLong){
$.ajax({
url: 'http://query.yahooapis.com/v1/public/yql',
data: {
q: "select * from json where url ='http://policeapi2.rkh.co.uk/api/locate-neighbourhood?q="+latLong+"'",
format: "json"
},
dataType: "jsonp",
success: function (data) {
alert(JSON.stringify(data));
},
error: function (result) {
alert("Sorry no data found.");
}
});
}