Calling PHP file with AJAX [duplicate] - javascript

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 8 years ago.
I have a php file that takes a array and an integer and returns a string.
I would like to get that string, however, when I log the result to the console, I see an object and don't see my string anywhere inside
result = $.ajax({ url: 'myPhpFile.php?firstargument=myArray&secondargument=myInteger' });
console.log(result);

You can try to next script:
$.ajax({
type: "GET",
url: "myPhpFile.php",
data: { firstargument: myArray, secondargument: myInteger }
})
.done(function(result){
console.log(result);
});

Related

Display certain text before executing ajax [duplicate]

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 8 months ago.
I don't succeed with outputting this text before executing ajax. I know it has something to do with async/await, but after reading other posts and articles I still can't figure it out. Anyone able to help me with providing an updated code?
This is the output (Calculating now...) I want before execution:
$(this).parent('td').parent('tr').find('td span.google').text('Calculating now...');
Full code:
$(function(){
$('.calculate').click(function(){
$(this).parent('td').parent('tr').find('td span.google').text('Calculating now...');
var total_distance = 0;
$(this).parent('td').parent('tr').find('td span.tripleg').each(function() {
var origin = $(this).attr('origin');
var destinations = $(this).attr('destinations');
$.ajax({
type: "GET",
url: 'includes/calculate_distance.php?action=distance_analysis',
data: {"origin": origin, "destinations": destinations, "google_api_key": google_api_key},
async: false,
success: function(result) {
total_distance = total_distance + parseFloat(result);
}
});
});
$(this).parent('td').parent('tr').find('td span.google').text(total_distance.toFixed(2));
});
});
Many thanks in advance!

Why is my variable coming back as undefined? [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
What is wrong with my script? It has a global variable which is later set with a function, but it comes back as undefined. Why is that?
<script>
var userName
function getAnonUserName() {
$.ajax({
url: "https://ck:8081/get-username",
type: "get",
success: function(response) {
userName = response
}
})
}
window.onload = function() {
getAnonUserName()
console.log(userName)
getAnonUserName is asynchronous, you have to wait for the response

Async Get Request return true [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 7 years ago.
I have the code:
if (venue_exists(instagramUserID)){
alert('A');
}else {
alert('C');
}
function venue_exists(instagramUserID) {
$.get( "/venues/" + instagramUserID, function(json) {
if (json.data.success){
alert('B');
return true;
}
}, "json" );
Currently my output is: B, C
I'm confused as to why the code is not going into A as true is being returned. Does this have something to do with the asynchronous request?
Any help would be greatly appreciated.
Yes, it happens because ajax is asynchronous. When you call venue_exists it returns undefined.
If you need to have that check you need to call it synchronously.
function venue_exists(instagramUserID) {
return JSON.parse($.ajax({
url: '/venues/' + instagramUserID,
type: "GET",
dataType: "json",
async: false
}).responseText);}

How to return data from ajax call JQuery [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 7 years ago.
I have a webapi in asp.net that gives me back JSON code. I want to access this with JQuery in a php-website.
I have this JQuery code to get the data from the webapi:
$.ajax({
type: 'GET',
url: 'localhost/webapi/api/data'
}).done(function (data) {
});
How can I return the value of 'data' to a global variable? So that I have a list of objects where I can loop through with navigation buttons.
Example:
When I click on the nextButton, I want to get the value of data[1].Text.
$('#nextButton').click(function() {
data[1].Text;
});
Perhaps like this:
var dataContainer = {}; // if you work with strings use ''
$.ajax({
type: 'GET',
url: 'localhost/webapi/api/data'
}).done(function (data) {
dataContainer = data;
});
$('#nextButton').click(function() {
if(dataContainer != {}){ // for strings != ''
// use dataContainer
}
});

Returning value from javascript object [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 8 years ago.
I have created a javascript object and im trying to fetch som data from it.
Im using jquery ajax to fetch send some values and then returing them from a php script.
this works, but when i try to use an instanse of my javascript object to dispaly the value i get 'undefined'.
Object:
var getDBresults = (function () {
function getResult(url,TableName ,callback){
$.ajax({
url: url,
type: 'POST',
data: {
'table':TableName,
},
success: function(data){
callback(data);
},
error: function(event){
alert(event.error);
}
});
}
return {
getAllVideoes: function(){
getResult("getAllResults.php", "videoer", function(data){
console.log(data); //Works
return data;
});
},
}
})();
in my other script:
var obj = getDBresults;
var data = obj.getAllVideoes(); //this runs, and produce the log showed above
console.log(data) //results in undefined
console.log(obj.getAllVideoes()) //results in undefined
Maybe you can use callbacks it will solve your async problems: http://recurial.com/programming/understanding-callback-functions-in-javascript/

Categories

Resources