This question already has answers here:
Variable doesn't get returned from AJAX function
(2 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 9 years ago.
I am trying to compare 2 arrays. One is taken from DOM, generated by .split(" "), the other I would like to be retrived by $.get call.
All is working fine if I test the dictionary array manually. Example:
// var dictionary = ["in", "the"]; // <- this one works
var dictionary = $.getJSON('job/dictionary/en');
$(this).keyup(function() {
var words = $(this)
.text()
.split(" "),
words_final = [],
i = 0;
jQuery.grep(words, function(word) {
if (jQuery.inArray(word, dictionary) == -1) words_final.push(word);
i++;
});
});
Response from this URL is Content-Type: application/json with response body:
[
"in",
"the"
]
Apparently... then I try to run $.getJSON('job/dictionary/en') in chrome dev tools, I get an object back with responseText: "["in","the"]"
I've tried with $.get() as well with the same result.
Any help really appreciated.
Related
This question already has answers here:
Need help filtering an array in JavaScript
(2 answers)
How to use OR condition in a JavaScript IF statement?
(8 answers)
Closed 6 months ago.
I have a set of data with location info. I am trying to filter only the data where Location matches 'India' OR 'US'. I am able to match a single criteria using the below:
var data = ws.getRange("A2:F" + ws.getLastRow()).getValues();
data = data.filter(function(r){ return r[location] == 'India'});
However, for multiple criteria, the below is what I have tried.
var data = ws.getRange("A2:F" + ws.getLastRow()).getValues();
data = data.filter(function(r){ return r[location] == '=OR("India","Singapore")'});
This does not return any error nor any result. Pls guide.
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
I have declared an empty array called answerDataArray and pushed values into the array (values are snapshots from firebase DB). When I can use console.log(answerDataArray) outside the forEach loop it showing an empty array but when I do console.log(answerDataArray) anywhere inside the forEach loop I get the populated array. Please tell what happening here.
var ideationContestRef = ideationDataRef.child(contestDataRef.key);
ideationContestRef.once("value", function(snapshot){
if(snapshot.child("answers").exists()){
snapshot.child("answers").ref.once("value", function(snapAnswerKey){
var answerKey = Object.keys(snapAnswerKey.val());
var answerDataRef = db.ref("answerData");
var answerDataArray = [];
answerKey.forEach(function(key){
answerDataRef.child(key).once("value", function(snapAnswerData){
answerDataArray.push(snapAnswerData.val());
});
});
console.log(answerDataArray);
// res.render("ideation", {id: contestId, layout: 'styled.handlebars', page: 'ideation', user_logged_in: user_presence, contestData: snapshot.val(), answerDataArray: answerDataArray});
});
}
Your problem probably is the fact that answerDataRef.child(key).once("value", function(snapAnswerData){ is async, therefore you can't expect to have values line after the forEach that you have mentioned.
Maybe you can use Promise in order to return async value.
This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 7 years ago.
Hello stackoverflow community, I need help with ajax $.get function. When I recieve array from getlist.php and i alert ir like this alert(data_list); everything works correctly. But when I try to alert like this alert(data_list.id) it doesn't work. Here is how my array looks like in console:
[{"id":"2","name":"Something","type":"horizontal","clicks":"0","start_date":"01/20/2016","end_date"
:"02/19/2016","status":"1","target":"http://","image_url":"http://","pre_exp_email":"0"},{"id"
:"1","name":"None","type":"horizontal","clicks":"2","start_date":"01/20/2016","end_date":"05/19/2016"
,"status":"1","target":"http://wps.us.lt","image_url":"http://....../wp-content/uploads/2016
/01/250by250ad.jpg","pre_exp_email":"0","group_id":["1"],"slots":{"1":"1"}}]
And here is myfunction which calls get function.
function get_list() {
jQuery.get("/wp-content/plugins/wp125/functions/getlist.php", { grouptype:jQuery('#grouptype').val() }, function(data_list){
var str;
alert(data_list.name);
});
}
If data is only a object like
var k={"id":123;"name":"abc"}
you can access id using k.id
but when it is object of Array like
var k=[{"id":123,"name":"abc"},{"id":13,"name":"ab"}];
in that case you have iterate over array something like this
for(i=0;i<k.length;i++)
{console.log(k[i].id)}
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 7 years ago.
I have a JSON data about 150 users, i have convert that into java script array, now what i want it to pass this array to PHP, So that i can perform further operations on this array.
var JSONString = '[{'user_id' : "1", "user_name" : "abc", "email":"abc#gmail.com"}, {"user_id":"2", "user_name":"abc", "email":"abc2#gmail.com"}]'
var JSONObject = JSON.parse(JSONString);
for (var key in JSONObject) {
if (JSONObject.hasOwnProperty(key)) {
console.log(JSONObject[key]["user_id"] + ", " + JSONObject[key]["user_name"]);
}
}
now what i need it to pass this array to PHP array variable , that will loop through and perform further operation.
i have seen some solution that display it on html with JavaScript, but i don't need that. i need this array object in php array variable.
Instead of parsing JSON string in JavaScript, pass it directly to PHP and use
PHP function
json_decode($json, true)
it will give you data in array format.
This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 8 years ago.
I am sending dictionary from python like this:
{'root':['value','path','type'],.......}
i am send it to javascript by ajax call request by serializing it,
How to access that dictionary in javascript.
thanks in Advance
Suppose you are making AJAX call in below way, you will get the response dict as resValue. use JSON.parse method on it
$.getJSON( "/url", {params }, function( data, status, xhr ) {
$.each(data.response, function(resKey, resValue){
if(resKey == "success"){
var _result = JSON.parse(resValue);
}
}
}