This question already has answers here:
async-ajax call not working as expected
(2 answers)
Ajax synchronous requests
(1 answer)
Closed 8 years ago.
So, I have a list of imdb ids stored in the database. What I want to do is, iterate over these and query a web service provider, for example TMDB and get the movie thumbnail associated with that imdb id.
I have fetched the data from the database, and stored it in response.
<script>
var results_area = $(".results-area");
function render(response) {
var img_thumbnail = $(".img-thumbnail");
var quote= $(".quote");
results_area.empty();
for (var i = 0; i < response.d.length; i++) {
img_thumbnail.attr('src', getImageThumbnail(response.d[i].imdbId));
quote.text("Reviews:" + response.d[i].quote);
results_area.append(img_thumbail.clone(),quote.clone());
}
}
function getImageThumbnail(imdbId) {
$.ajax({
async: false,
url: "https://api.themoviedb.org/3/movie/" + imdbId + "?",
data: param,
dataType: 'jsonp',
success: function () {
}
});
}
</script>
Since my dataType is 'jsonp', async:false won't work. What is happening is since in my html img src="#", it stays the same after the for loop finishes.
Is there a better way to do this?
Related
This question already has answers here:
Javascript POST not working - Sending Javascript array to PHP
(4 answers)
Closed 4 years ago.
Trying to have data passed to a php script so the said data can be added to the session.
The debug console log returns are the following:
the quant array is correct and typeof is object, the JSON.stringified data's type is string , lastly success from the ajax success.
In the PHP script var_dump return is NULL
$('#bigsubmit').click(function() {
var quant = [];
$('.input-number').each(function() {
var tmp = {};
tmp.id = this.id;
tmp.qu = $(this).val();
quant.push(tmp);
});
console.log(quant);
var data = JSON.stringify(quant);
console.log(typeof(data));
$.ajax({
type: "POST",
url: url,
data: {
data: data
},
success: function() {
console.log("success");
}
});
the php script (url var)
<?php
session_start();
$_SESSION['test'] = $_POST['data'];
var_dump($_SESSION['test']);
?>
Your success callback function isn't taking in a parameter, try changing to this,
success:function(data) {
console.log(data);
}
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 6 years ago.
Im retriving data from my php and its working fine , the problem is that
var myData is getting cached and only updates if i refesh my page 2 times...
on my firebug i can see the post updates but when i `console.log(myData);the data is the old one ..until i refresh..
$.ajax({
url: "http://www.example.com/mobile_read.php", // path to remote script
dataType: "JSON",
type: 'post',
data:{
id : id,
eventos : eventos,
}, // data set to retrieve JSON
success: function (data) { // on success, do something...
// grabbing my JSON data and saving it
// to localStorage for future use.
localStorage.clear();
localStorage.setItem('myData1', JSON.stringify(data));
}
});
var myData = JSON.parse(localStorage.getItem('myData1'));
console.log(myData);
var arrayObjects = myData;
You're probably trying to set and read mydata before the request/response is complete. Instead, move that into your success callback.
$.ajax({
url: "http://www.example.com/mobile_read.php", // path to remote script
dataType: "JSON",
type: 'post',
data:{
id : id,
eventos : eventos,
}, // data set to retrieve JSON
success: function (data) { // on success, do something...
// grabbing my JSON data and saving it
// to localStorage for future use.
localStorage.clear();
localStorage.setItem('myData1', JSON.stringify(data));
var myData = JSON.parse(localStorage.getItem('myData1'));
console.log(myData);
var arrayObjects = myData;
}
});
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 8 years ago.
I'm trying to append the link value of Instagram images into an array. The link values are showing up fine in the console but my array length is 0 after the ajax function. Also '0' shows up in the console before all the links.
I have an idea this problem is caused by ajax running asynchronously and so the rest of the js code is actually run before anything can be appended to $images, but I have no idea how to circumvent this. This is my code:
$liked = 'https://api.instagram.com/v1/users/self/media/liked?access_token=' + $access_token + '&COUNT=20';
$images = []
$.ajax({
type:'get',
dataType:'jsonp',
cache: false,
url: $liked,
success: function(data) {
for (var i = 0; i < data.data.length; i++) {
console.log(data.data[i].images.standard_resolution.url);
$images.push(data.data[i].images.standard_resolution.url);
}
}
});
console.log($images.length);
The console.log will run before the ajax call is ended; write your code or you console.log in your success function right after the end of the data loop.
Like:
$liked = 'https://api.instagram.com/v1/users/self/media/liked?access_token=' + $access_token + '&COUNT=20';
$images = []
$.ajax({
type:'get',
dataType:'jsonp',
cache: false,
url: $liked,
success: function(data) {
for (var i = 0; i < data.data.length; i++) {
console.log(data.data[i].images.standard_resolution.url);
$images.push(data.data[i].images.standard_resolution.url);
}
console.log($images.length); // here the array is filled with response data
}
});
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 9 years ago.
I'm trying to write a JavaScript function that retrieves surname that corresponds to a supplied email address with no success so far. Here's my code:
function getUsername(){
var username;
$(function(){
var dataString = 'email=' + EMAIL_ADDRESS;
$.ajax({
url: 'user_crud_operations.php',
data: dataString,
dataType: 'json',
success: function(data){
U = data[1];
username = data;
}
});
});
return username;
}
I need to use the retrieved username in another function elsewhere. Where am I going wrong?
Asynchronous functions (as used in AJAX) usually do not return values but instead trigger callback functions.
You must restructure your design so that "getUserName()" passes to a callback instead of returning a value. For example:
function getUsername(handler) {
// ...
$.ajax({
// ...
}).done(function(username) {
if (handler && (typeof(handler)==='function')) {
handler(username);
}
});
}
// Sample usage...
getUsername(function(username) {
alert("OK: username=" + username);
});
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 9 years ago.
I have an array declared in an else structure
var days = new Array();
$.ajax({
url: 'someurl',
dataType: 'jsonp',
success: processJSON
});
I process the data from the ajax call in the processJSON function
function processJSON(jsonData) {
var weather = jsonData.data.weather;
for(var key in weather) {
var day = weather[key];
days.push(new Array(day.tempMaxC, day.type));
}
}
I add some other arrays to the days-array.
If I check it in the console (in the processJSON-function), the length is three/
Then when I want to use the variable in some code under the the ajax-call, nothing works and when I check the length it's 0. I guess it's something with declaration?
Is your code under the Ajax call happening after the processJson function? The call to the ProcessJson function is asynchronous and happens only after the Ajax call finishes. However the code following it might happen before. So it might be a timing thing. If you need the array for any other processing you should have it in the callback or after you know the callback has been finished.
Put the declaration outside of the condition/else
var days = new Array();
if(...){
}else{
$.ajax
}
You need to wait until the XHR call has finished. It looks like you're using JQuery so check out their deferred docs (http://api.jquery.com/deferred.then).
You would end with something like this:
$.ajax({
url: 'someurl',
dataType: 'jsonp'
}).then(function( jsonData ) {
var weather = jsonData.data.weather;
for(var key in weather) {
var day = weather[key];
days.push(new Array(day.tempMaxC, day.type));
}
console.log( 'done!', days );
});