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");
});
}
Related
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
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.
<script>
(function(){
var searchURL = 'http://en.wiktionary.org/wiki/search';
$.ajax({
type: "GET",
url: searchURL,
dataType: "jsonp",
cache: false,
async:false,
success: function(responseData, textStatus, XMLHttpRequest){
iframe(responseData);
}
});
})();
</script>
I added this script to my html file and it is show the following errors, copy pasting the function in console is also showing the same errors.
Uncaught SyntaxError: Unexpected token <
Resource interpreted as Script but transferred with MIME type text/html
could anyone help me resolve this issue, I am using Chrome brower.
You can't request arbitrary pages via AJAX, and jsonp doesn't magically make that work. You need to use the Wiktionary API.
The URL is http://en.wiktionary.org/w/api.php.
$.ajax({
url: 'http://en.wiktionary.org/w/api.php',
dataType: 'jsonp', // will automatically add "?callback=jqueryXXX"
cache: true, // the API complains about the extra parameter
data: { // the parameters to add to the request
format: 'json',
action: 'query',
titles: 'test'
},
success: function(data){
console.log(data);
}
});
Logging rides alerts, logging users doesn't.
I can't figure out what's going on here... There's no network errors. Both data/users.json and data/rides.json are reaching the client fine.
$.ajax({
url: "data/users.json",
dataType: 'json',
async: false,
success: function(data){
users = data.users
alert('Logging Uers');
}});
$.ajax({
url: "data/rides.json",
dataType: 'json',
async: false,
success: function(data){
alert('Logging rides');
rides = data.rides
}});
Maybe users = data.users it's throwing an exception (caused by a possibly undefined data). Did you tried to comment that line?
Also, you should implement an error callback:
$.ajax({
url: "data/users.json",
dataType: 'json',
async: false,
success: function(data){
//users = data.users;
alert('Logging Users');
},
error: function() {
alert("ERROR");
console.log(arguments);
}
});
Someone had invalidated the json file. I fixed the syntax errors and it's working now.
You may be getting a syntax error in both functions because of the lack of a semicolor after the assignment. However, in your rides function, the assignment is after the alert, allowing the alert to fire. In users, it is not. Hence no alert.
I have a problem when using jsonp to API HTTPS website.
it will return below
response=1&responsetext=SUCCESS&authcode=123456&transactionid=1592337329&avsresponse=&cvvresponse=&orderid=&type=sale&response_code=100&merchant_defined_field_6=&merchant_defined_field_7=&customer_vault_id=
and this is my code.
function getJSON() {
$.ajax({
type: "POST",
dataType: 'jsonp',
data:{},
jsonp: true,
jsonpCallback: "callbackName",
url: 'https://secure.equitycommercegateway.com/api/transact.php?username=test123&password=test1234&ccnumber=4111111111111111&ccexp=1012&amount=10.00&type=sale&product_sku_1=monthly&callbackName=?',
success: function(msg){
alert(msg);
}
});
}
$(document).ready(function(){
var callbackName = function(data) {
//alert(data.listing.id );
}
getJSON();
});
It shows me a console error, Error to read response text..
and points to (=) symbol right before "SUCCESS" text.
anyone can help me?
It is not possible to make a JSONP POST request see this post.