how to get image url from "https://api.qwant.com/api/search/images?count=10&offset=1&q=cars" api using jquery. i'm unable to do it. bellow i've attached my code
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$.ajax({
url:"https://api.qwant.com/api/search/images?count=10&offset=1&q=cars",
type:"GET",
crossDomain : true,
async: false,
dataType: "jsonp",
jsonpCallback: "myJsonMethod",
success: function(json) {
$.parseJson(json)
},
error: function(e) {
console.log(e);
}
});
function myJsonMethod(response)
{
console.log(response);
}
</script>
Your request isn't working because of CORS, which is enabled on the API server. You need a proxy server to workaround this. For development purposes you could use a free online proxy server, your code then simplifies:
$.ajax({
url:"<PROXY:SERVER>https://api.qwant.com/api/search/images?count=10&offset=1&q=cars",
success: function(json) {
// Do stuff with data
},
error: function(e) {
console.log(e);
}
});
As an example check out this working fiddle.
Try this
$.ajax({
url:"https://api.qwant.com/api/search/images?count=10&offset=1&q=cars",
type:"GET",
crossDomain : true,
async: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
jsonpCallback: "myJsonMethod",
success: function(json) {
$.parseJson(json)
},
error: function(e) {
console.log(e);
}
});
This is a CORS thing, so you can server all this up from a web server, like http-server, and I think certain browsers like Firefox allow this to occur locally.
There are some flags with Chrome that'd allow it to work locally like this too, I believe.
Cheers
Related
I am currently trying to obtain this JSON array from
http://d.yimg.com/aq/autoc?query=goo®ion=US&lang=en-US
However, which ever I use, it does not seem to work.
Using
$.ajax({
type: 'GET',
url: 'http://d.yimg.com/aq/autoc?query=goo®ion=US&lang=en-US',
cache : false,
dataType : 'json',
success: function(data){
alert('got it!');
}
}
And also tried
$.getJSON("http://d.yimg.com/aq/autoc?query=goo®ion=US&lang=en-US, function(data) {
});
As well as looking into a bypass for cross domain, and I tried using a proxy, not did not work, Maybe I am implementing it wrong? I can not seem to properly obtain the data (nothing return to data).
EDITED
I used this bypass php proxy to try and bypass the missing CORS and JSONP, and it does not seem to return the data correctly either (I followed an example). The php is within my directory.
$(document).ready(function() {
$("#tickBox").on("input", function(e) {
'use strict';
var tick = document.getElementById("tickBox").value;
$.ajax({
type: 'GET',
url: 'http://d.yimg.com/aq/autoc?query=goo®ion=US&lang=en-US',
crossOrigin : true,
//dataType : 'json',
context: {},
success: function(data){
alert(data.ResultSet.Result[0].name);
}
});
});
});
I've got a room monitor device I'm collecting data from, I can get it work with javascript but not jquery.
With plain javascript, define a function:
function myfunction(data){
console.log(data);
}
Then in the page:
<script type="text/javascript" src="http://172.16.198.19/getData.jsonp=callback=myfunction"></script>
I get an object in console containing all the data. Great!
I now try to get the same result using jQuery's $.ajax but am having problems:
$.ajax({
url: 'http://172.16.198.19/getData.jsonp',
dataType: 'jsonp',
jsonCallback: 'parseData',
success: function(data){
console.log(data);
},
error: function(){
console.log("nope");
}
});
This gives me the following error:
Uncaught ReferenceError: Invalid left-hand side in assignment
Any suggestions on what to try / how to fix are appreciated. Thanks.
Edit: Solved and answered. jQuery formats the query with _= in it which the server was rejecting. Working function is thus:
$.ajax('http://172.16.198.19/getData.jsonp', {
type: 'get',
dataType: 'jsonp',
jsonp: false,
jsonpCallback: 'parseData'
}).done(function(data) {
console.log(data.sensor[0].tc);
}).fail(function() {
console.log("nope");
});
}
This has been solved thanks to the very helpful Cork in #jquery on freenode.
The problem was jquery formatting the query with _= in it which the server was rejecting.
The working result is thus:
$.ajax('http://172.16.198.19/getData.jsonp', {
type: 'get',
dataType: 'jsonp',
jsonp: false,
jsonpCallback: 'parseData'
}).done(function(data) {
console.log(data);
}).fail(function() {
console.log("nope");
});
}
I have the following code:
$("form").submit(function()
{
//Checking data here:
$("input").each(function(i, obj)
{
});
alert(JSON.stringify($(this).serializeArray()));
var url='http://127.0.0.1:1337/receive';
$.ajax({
url: url,
type: 'POST',
contentType:'application/json',
data: JSON.stringify($(this).serializeArray()),
dataType:'json'
});
});
And after I submit the form, I get a JavaScript alert with the json string, so that is made correct (on my server I only log it so it does not matter what it is in it). If I try to send a request to the same link from postman it works, it logs it.
I think I'm doing something wrong in the ajax call, but I am not able to figure out.
Try below piece of code. Add success and error handler for more details
$("form").submit(function()
{
//Checking data here:
$("input").each(function(i, obj)
{
});
alert(JSON.stringify($(this).serializeArray()));
var url='http://127.0.0.1:1337/receive';
$.ajax({
url: url,
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify($(this).serializeArray()),
dataType:'json',
success : function(response) {
alert("success");
},
error: function (xhr, status, error) {
alert(error);
}
});
});
data:{ list : JSON.stringify($(this).serializeArray())}
From the Jquery docs:
Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol.
crossDomain attribute simply force the request to be cross-domain. dataType is jsonp and there is a parameter added to the url.
$.ajax({
url:'http://127.0.0.1:1337/receive',
data:{ apikey: 'secret-key-or-any-other-parameter-in-json-format' },
dataType:'jsonp',
crossDomain: 'true',
success:function (data) {alert(data.first_name);}
});
I've made this ajax call:
$.ajax({
//query rest che trova tutti gli amici dell'utente corrente
type: 'GET',
async: false,
url: "http://www.timeapi.org/utc/now",
success: function(data) {
time_now=data;
},
error: function(data) {
console.log("ko" );
}
});
in chrome works perfectly but in firefox and on mobile android goes in error callback.
In firefox the error is(written in red):
GET 'http://www.timeapi.org/utc/now.json 200 OK
This server must be old, it doesn't set any cross-origin authorization header.
But luckily, the home page explains it's JSONP compatible and gives an example :
<script type="text/javascript">
function myCallback(json) {
alert(new Date(json.dateString));
}
</script>
<script type="text/javascript" src="http://timeapi.org/utc/now.json?callback=myCallback"></script>
You can also adapt your code :
$.ajax({
type: 'GET',
dataType: "jsonp",
url: "http://www.timeapi.org/utc/now.json?callback=?",
success: function(data) {
time_now=data.dateString;
},
error: function(data) {
console.log("ko" );
}
});
Note also that I removed the async:false. Not only is it incompatible with JSONP, it's also always a bad idea.
I am trying to call a web-service which has a Json output from JavaScript. But I am unable to get the value. I tried with different methods but have been unsuccessful. Please help !!
Here is the code I tried
<script src="jquery-1.3.2.min.js"></script>
<script type="text/javascript">
function callajax(){
var html =$.ajax({
cache: false,
type: "GET",
async: false,`enter code here`
data: {},
url: "http://domain/abc.php?param=abcd',
contentType: "application/json; charset=utf-8",
dataType: "json",
crossDomain: true,
Success: SucceedFunc,`enter code here`
error: function (request, errorText, errorCode)
{
alert(errorText+"--"+errorCode);
}
});
}
function SucceedFunc(data, status)
{
alert("Enter into Success");
alert(data);
}
Desired output is in {"name":Alex,"Success":true} format.
I need to pick value for "name".
Help would be appreciated.
Is it because your Success: is capitalized? Should be success:.
EDIT:
Also, is there a reason you're using such an old version of jQuery? They're at 1.9.x now (for most uses). I just looked it up and the crossDomain option was added in 1.5.