javascript async not running - javascript

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.

Related

Jquery ajax jsonp: Invalid left-hand side assignment

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");
});
}

jQuery ajax return undefined instead of promise

1.8.2
$.ajax({
url: "/someurl/",
async: true,
dataType: 'json'
}).done(function ( data ) {
if( console && console.log ) {
console.log("Sample of data: ", data);
}
});
causes error "Uncaught TypeError: Cannot call method 'done' of undefined", but request sends and server response comes to me with data!
If I wrote
$.ajax({
url: "/someurl/",
async: true,
dataType: 'json',
success: function (data) { console.log(data); }
});
That's ok and console.log fires!
It seems likely that your error probably comes from the way your using the response data, because your snippet is correct.
Why don't you run a fail method and test for errors there?
$.ajax({
url: "/someurl/",
async: true,
dataType: 'json'
}).fail(function (error) {
console.log(error);
});
Alternatively, check your Network Tab for xhr response preview?

How to store JSON response into JavaScript

I'm trying to store the JSON response in to variable so that I can process it later, i tried with following but getting null,
function go(){
var jsonObj = getJson(url);
alert(JSON.stringify(jsonObj));
}
function getJson(){
return JSON.parse($.ajax({
type: 'POST',
url: 'url',
dataType: 'json',
global: false,
async: false,
success: function(data){
return data;
}
}).responseText);
}
mean time i can see the JSON content getting printed on my server console.
EDIT
Thannks rid..! that fixed the issue. you solution works fine in IE8, but for Chrome and FireFox still getting 'null' in alert pop-up. i don't think its browser specific fix..?
$.ajax({
type: 'POST',
url: 'url',
dataType: 'json',
global: false,
async: false,
success: function(data){
// do some stufs or assign data to a var
window.myVar = data;
}
});
Ajax is asynchronous ,so you need to store the response in a variable in the callback.
in this exemple , window.myVar is a global variable that will contain the response data. You cant access the variable until a response has returned , you'll need to write async code.

Show a loading div during ajax post

I have a mobile app using mostly JQuery Mobile. I have an ajax function using POST and I can't seem to get anything to effect the UI when I fire the click event. I tried setting
$('#cover').show();
as the very first thing in the function then I do some basic things like document.getElementById('user') etc to set some variables and check input, but as long as the ajax function is there it won't show the div or even the spinner from JQ Mobile. Unless I debug and step through the code then the spinner and div show up fine. I tried setTimeout and putting it in the beforeSend area of the ajax call. Everything works fine otherwise. It seemed to work a little better with GET I'm not sure if that has anything to do with it or not.
$.ajax({
cache: false,
type: "POST",
async: false,
url: urlString,
data: jsonstring,
contentType: "application/json",
dataType: "json",
success: function (data) {
JSONobj = JSON.parse(data);
},
beforeSend: function(xhr){
//console.log('BeforeSend');
},
complete: function (xhr) {
//console.log('Complete');
},
error: function (xhr, status, error) {
console.log(error);
}
});
You could use the Ajax Global handlers to handle this:
$(document).
.ajaxStart(function(){
$('#cover').show();
})
.ajaxStop(function(){
$('#cover').hide();
});
This way you don't have to worry about showing/hiding the overlay on individual Ajax calls.
Try this
$("#someButton").click(function(e){
e.preventDefault() //if you want to prevent default action
$('#cover').fadeIn(100,function(){
$.ajax({
url: "someurl",
data: "Somedata",
contentType: "application/json",
dataType: "json",
},
success: function (data) {
JSONobj = JSON.parse(data);
$('#cover').fadeOut(100);
},
complete: function (xhr) {
$('#cover').fadeOut(100);
}
});
});
});

Error when returning value using JSONP

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.

Categories