I have two var's
var a = result.master_id;
var b = result.session_name;
i want to create a json string to pass to ajax. i tried this
var userData = {'pid':a,'session' :b};
i'm storing this userData in a sessionStorage and sending to ajax . but i'm getting pid undefined error.
is this the right way to do this?
any help is appreciated.
regards,
newbie
You are using the right code to store into variable. Try to console variable to identify.
Example code given below:
let result = {
master_id: 1,
session_name: "Hardik"
}
var a = result.master_id;
var b = result.session_name;
var userData = {'pid':a,'session' :b};
console.log(userData);
Related
This has probably been asked before but I can't seem to find any answers that I can get to work.
I have a SQL query that produces five rows of data in the backend.
And I then fetch the data from the query like so:
if (reader.HasRows) {
var list = new List < GetUserNames > ();
// Read advances to the next row.
while (reader.Read()) {
//Person p = new Person();
// To avoid unexpected bugs access columns by name.
//Convert.ToInt32(dataReader.GetValue(3));
wholeAvg = Decimal.Round(reader.GetDecimal(reader.GetOrdinal("wholeavg")), 2);
list.Add(new GetUserNames {
users = reader.GetString(2)
});
allRecords = list.ToArray();
}
}
Now what I would like to send to the client and use with javascript is the allRecords array.
Can someone point me in the right direction how to get the array from the backend?
Thanks!
You could serialize it to JSON then store the value in a hidden input. In your javascript you can then pick up the value of that field:
.cs:
var json = JsonConvert.SerializeObject(allRecords);
hdUsers.Value = json;
.aspx:
<asp:HiddenField runat="server" ID="hdUsers"/>
<script type="text/javascript">
var jsonString = $("[id$='hdUsers']").val()
var arr = JSON.parse(jsonString);
</script>
If you are using MVC, you can add 'allrecords' to TempData and use following code;
var records = #Html.Raw(Json.Encode(TempData["Records"]))
how can i get the first "id_miembro" for example of this:
{"actividades":[{"id_miembro":"V-005","id_dpto":"AC-04","id_actividad":"D-01","id_seccion":"S-03"},{"id_miembro":"V-005","id_dpto":"AC-02","id_actividad":"D-01","id_seccion":"S-01"}]}
I've tried with other ways but nothing, thanks.
Parsing a JSON string in javascript can be done with JSON.parse(). For your JSON you can do this to get the id_miembro of the first item in the list.
var json = '{"actividades":[{"id_miembro":"V-005","id_dpto":"AC-04","id_actividad":"D-01","id_seccion":"S-03"},{"id_miembro":"V-005","id_dpto":"AC-02","id_actividad":"D-01","id_seccion":"S-01"}]}';
var obj = JSON.parse(json);
var actividades = obj.actividades;
var first = actividades[0];
var id_miembro = first.id_miembro;
I broke it down into seperate variables so it's easier to follow which line does what.
If your data is alread a javascript object you can skip the first two lines and just do this
var obj = {"actividades":[{"id_miembro":"V-005","id_dpto":"AC-04","id_actividad":"D-01","id_seccion":"S-03"},{"id_miembro":"V-005","id_dpto":"AC-02","id_actividad":"D-01","id_seccion":"S-01"}]};
var actividades = obj.actividades;
var first = actividades[0];
var id_miembro = first.id_miembro;
I'm having a trouble with getting access to an object's property.
Isn't it possible to get access to an object's property like this?
key["heading"]
key in the code above is a variable.
This code below is the code I'm working on right now.
alertHeading.on('blur', function(){
var inputtedVal = $(this).val();
var key = alertMode.val();
chrome.runtime.getBackgroundPage(function(backgroundPage) {
var background = backgroundPage.background;
//(1)This works fine.
background.setStorage(key, {heading:inputtedVal});
console.log(background.getStorage(key));// Object {heading: "aaa"}
//(2)This doesn't work.
var alertObject = background.getStorage(key["heading"]);
console.log(alertObject);// null. I'm expecting to get "aaa".
});
})
I think I'm making a very simple mistake which comes from my lack of javascript knowledge.
Please help me out to solve this problem.
Your key isn't an object, it's a string. It is the return from background.getStorage(key) that is an object, so you can do this:
var alertObject = background.getStorage(key)["heading"]; // note () and [] placement
// OR, in two steps:
var alertObject = background.getStorage(key);
var heading = alertObject["heading"];
EDIT:
"I haven't understood why it's not an object but a string yet"
Your key variable is set to the return from jQuery's .val() method:
var key = alertMode.val();
...which returns a string that is the value of the form element that it is called on. Add in a console.log(key) and you'll see.
Cannot grab the follower count from the following URL. Shows up as blank. Am I grabbing the right parameters?
https://developer.foursquare.com/docs/explore#req=pages/search%3Fname%3DNintendo
function foursquareFriends(url) {
var responses = UrlFetchApp.fetch(url);
var json = responses.getContentText();
var data = JSON.parse(json);
var likes = data.response.results.followers.count;
return likes;
}
You should do this (using this)
data.response.results[0].followers.count;
(And of course if you have more than 1 value - you should iterate).
What I mean by that is say I have JSON data as such:
[{"ADAM":{"TEST":1}, "BOBBY":{"TEST":2}}]
and I want to do something like this:
var x = "ADAM";
alert(data.x.TEST);
var data = [{"ADAM":{"TEST":1}, "BOBBY":{"TEST":2}}],
x = "ADAM";
alert(data[0][x].TEST);
http://jsfiddle.net/n0nick/UWR9y/
Since objects in javascripts are handled just like hashmaps (or associative arrays) you can just do data['adam'].TEST just like you could do data.adam.TEST. If you have a variable index, just go with the [] notation.
var data = [{"ADAM":{"TEST":1}, "BOBBY":{"TEST":2}}]
alert(data[0].ADAM.TEST);
alert(data[0]['ADAM'].TEST)
if you just do
var data = {"ADAM":{"TEST":1}, "BOBBY":{"TEST":2}};
you could access it using data.ADAM.TEST and data['ADAM'].TEST
That won't work as you're setting x to be a string object, no accessing the value from your array:
alert(data[0]["ADAM"].TEST);
var data = [{"ADAM":{"TEST":1}, "BOBBY":{"TEST":2}}],
x = "ADAM";
alert(data[x].TEST);
This is what worked for me. This way you can pass in a variable to the function and avoid repeating you code.
function yourFunction(varName, elementName){
//json GET code setup
document.getElementById(elementName).innerHTML = data[varName].key1 + " " + data.[varName].key2;
}