Use variable out of ajax - ajax

How I can use count variable out of ajax? In ajax function "count" shows count, but dessous nothing.
var count;
$.ajax({
cache : false,
dataType: 'json',
type : "POST",
url : "count.php",
success : function(tdata){
count = tdata;
console.log(count); //this works
}
});
console.log(count); //this doesn't work

$.ajax() is async, you need to wait for it to finish.
var count;
$.ajax({
cache : false,
dataType: 'json',
type : "POST",
url : "count.php",
success : function(tdata){
count = tdata;
console.log(count); //this works
}
})
.done(() => {
// this code runs after ajax is resolved
console.log(count);
});
Refer to http://api.jquery.com/jQuery.ajax/ for other chaining methods

Related

URL for result of AJAX

I have AJAX for sort.
$.ajax({
url : orderby_params.ajaxurl,
data : data,
type : 'POST',
beforeSend : function(xhr){
},
success : function(data){
if(data){
$('.list-thumbs').html(data);
}
}
});
Everything works fine.
How can I get URL for AJAX result?
Such as: https://domen.com/shop/?orderby=price

Weird object returned from AJAX request

I have this method:
var chineseCurrency = getChinese();
function getChinese(){
return $.ajax({
context: this,
type: 'GET',
dataType: 'json',
url: "https://www.cryptonator.com/api/ticker/usd-cny"
});
}
That is what printed when console.log(chineseCurrency);:
I am not able to make chineseCurrency equal to "price", so it would be "6.80071377". How can I do that? Tried chineseCurrency.responseText, nope, chineseCurrency['responseText'], nope. Tried to JSON.parse(chineseCurrency), nope. Nothing works!
Sorry if repeated, couldn't find any answer at Stackoverflow.
How do I return the response from an asynchronous call?
Data that is received as response to asynchronous ajax call cannot be returned from the function that calls $.ajax. What you are returning is XMLHttpRequest object (see http://api.jquery.com/jquery.ajax/) that is far from the desired data.
var chineseCurrency = null;
function getChinese(){
return $.ajax({
context: this,
type: 'GET',
dataType: 'json',
url: "https://www.cryptonator.com/api/ticker/usd-cny",
success: function(data) {
alert("success1: chineseCurrency=" + chineseCurrency);
chineseCurrency = data.ticker.price;
alert("success2: chineseCurrency=" + chineseCurrency);
// do what you need with chineseCurrency
}
});
}
You are not taking the data from that is returned from the Ajax call. instead you are just returning the ajax object.
Change your code to :
$.ajax(
{
context: this,
type: 'GET',
dataType: 'json',
url: "https://www.cryptonator.com/api/ticker/usd-cny"
data :{},
error : function(data)
{
console.log('error occured when trying to find the from city');
},
success : function(data)
{
console.log(data); //This is what you should return from the function.
}
});

How to pass parameters without using url

I have number of values.
I need to pass these values to another page without using the window.location
function sample(cID){
var clg = ${param.clg};
$.ajax({
type : "post",
url : "sampleShow?clg="+clg+"&cID="+cID+"&level="+level,
dataType : "json",
cache : false,
beforeSend : function(xhr) {
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
},
success : function(response) {
window.location= "cc"
},
error : function(xhr) {
console.log("error"+xhr.status);
},
complete : function() {
}
});
}
This is my controller ajax function:
#RequestMapping(value = "sampleShow", method = RequestMethod.POST)
public #ResponseBody
String showc(HttpServletRequest request,Model model)
{
model.addAttribute("ccID", request.getParameter("cID"));
model.addAttribute("clg", request.getParameter("clg"));
model.addAttribute("level", request.getParameter("level"));
return "{\"sucess\":\"true\"}";
}
I need to get the values in cc page.
Your ajax call should like:
$.ajax({
type : "post",
url : "sampleShow",
data : {clg: clg, cID: cID, level: level },
success : function(response) {
window.location= "cc"
},
error : function(xhr) {
console.log("error"+xhr.status);
},
complete : function() {
}
});
No need to specify dataType: "json", as you are not sending json. Data is passed using 'data'. Refere jQuery.ajax() for more details.
You can also use shorthand method jQuery.post(), to post instead of jQuery.ajax()
You may also like to read on what-is-the-difference-between-post-and-get.

access json object returned from php file via ajax

$(function() {
var sineData;
$.ajax({
url : '../_php/loadFromDB.php',
type : 'POST',
data : 'getSines',
dataType : 'json',
success : function (result) {
console.log(result);
sineData=result;
},
error : function () {
alert("error");
}
})
});
Under Console->All->Response in firebug I get the below as expected:
[{"userID":"1","email":"user#mail.com","number":"800.256.6547","ext":"5788","startDay":"Sunday","endDay":"Thursday"}]
but when I look at sineData it is Undefined.
I want to be able to access these values like sineData[0].email
Where am I going wrong?
async: false, ended up fixing this for me but probably isnt the best solution for most aplications.
You are probably accessing sineData outside the ajax call. You are trying to access it before the asynchronous call is done. Try this:
function whenIsDone(result){
// Do whatever you want with the variable result
console.log(result[0].email);
}
$.ajax({
url : '../_php/loadFromDB.php',
type : 'POST',
data : 'getSines',
dataType : 'json',
success : whenIsDone,
error : function () {
alert("error");
}
})

extjs return ajax response

I need to assign the ajax response to a global variable so that i can use it thoughout my application. The obvious problem is that ajax requests are async thus complicating things.
I have tried to initialize an object before the ajax call, then assigning the response fron inside the success but that didn't work either.
Any ideas?
Example code
var p = {};
loadLang('en');
function loadLang(code) {
Ext.Ajax.request({
url : '/LoadLanguage.html',
method : 'POST',
params :{'code':code},
timeout : 250,
success : function(response, opts) {
obj = Ext.decode(response.responseText);
p = obj;
},
callback : function(o,s,r)
{
}
});
}
var myValue={item:"pie"};
Ext.Ajax.request({
url: 'test.php',
params: {
id: 1
},
success: function(response){
alert(response);
window.myValue.response=response; //yay for global scope.
console.log(window.myValue);//This has to be here,
//else it gets logged before the call gets completed.
}
});

Resources