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"]))
Related
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);
This is my data structure
.
Now I want to get all the ids from this database.
In MySQL, I could easily get it using this simple query
SELECT id
From sensor_data
But in firebase, I don't know how to do it.
I've tried
firebase.database().ref('/sensor_data').once('value', function(
snap.forEach(function(obj){
var date = obj.val().date;
var time = obj.val().time;
var id = obj.val().id;
var ph = obj.val().ph;
var temp = obj.val().temp;
})
});
But it needs another loop to separate the desired data.
If you know the id, you can query it directly like so:
firebase.database().ref(`/sensor_data/{$id}`).once('value').then(snapshot => console.log(snapshot.val(), snapshot.val().id));
If that's not the case, you indeed need to get the collection and then iterate like you've done, or try it like this:
firebase.database().ref('/sensor_data').once('value').then(data => {
for(let record of data) {
console.log(record.id);
}
});
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).
I am using GAS HTMLService to design a Web App and have hit a major hurdle that I can't seem to overcome -- so I'm hoping to get some help from y'all.
I'm able to create the template and display the HTML properly using the standard doGet:
function doGet(e){
var ss = SpreadsheetApp.openById('SpreadSheetKey');
var usersheet = ss.getSheetByName('UserInfo');
var lastRow = usersheet.getLastRow();
var Userrange = usersheet.getRange(2, 1, lastRow-1, 4);
var Uservalues = Userrange.getValues();
var length = Uservalues.length;
var template = HtmlService.createTemplateFromFile('Form.html');
template.stuff = {};
template.stuff = Uservalues;
template.action = ScriptApp.getService().getUrl();
return template.evaluate();
}
The HTML will render up fine so long as I remove this piece of code from my HTML file:
$('#chkP').click(function(){
var email = '<?=email?>';
var usename = $('#myuserName').val();
for(var i=0;i<num;i++){
var name = '<?!=stuff[i][0]?>';
alert(name);
}
});
The issue appears to be related to trying to use the 'i' variable -- I can get the proper info using:
'<?=stuff[2][0]?>' // THIS WORKS
<?=stuff[i][0]?> //THIS DOES NOT -- IT IS ALWAYS undefined
// it's like 'i' and <?stuff?> aren't in the same scope??
so I have the theory that I have a ServerSide array:
(Spreadsheetapp.blahblah.getValues();)
that is not available for the Client Side to play with.
My question to you folks is how do I get this data from the Spreadsheet into the HTML form and available to iterate through as an array using a simple 'for' loop....
OR is there a better way?
You are correct that the array is on the server. Any code inside <? .. ?> is server code) and not available on the client.
What you can do is copy the entire array to the client, so that you can iterate through it later:
Suppose you had an array on the server:
<? var serverStuff = [1, 2, 3]; ?>
You can tell the template to copy it over to the client (basically, write the values of it directly into the client code.
<script>
...
var clientStuff = <?!= JSON.stringify(serverStuff) ?>;
...
// Now you have it on the client! You can use it here:
alert(clientStuff[0]);
</script>
You can see what is happening by logging the content of the template (after evaluating) on the server:
Logger.log(template.evaluate().getContent());
Which would give you this actual client code, with the copy of the array written into it:
<script>
...
var clientStuff = [1, 2, 3];
...
// Now you have it on the client! You can use it here:
alert(clientStuff[0]);
</script>
I have the following code:
<script type="text/javascript">
var checksSinceLastPostBack = new Array();
function clientSelectedIndexChanged(sender, eventArgs) {
var ajaxManager = $find("<%= RadAjaxManager.GetCurrent(Page).ClientID %>");
var serializedCheckData = checksSinceLastPostBack.serializeArray();
if (ajaxManager != null)
ajaxManager.ajaxRequest(serializedCheckData);
}
</script>
The
var serializedCheckData = checksSinceLastPostBack.serializeArray();
doesn't seem to work. Am I misunderstanding this?
Also if this works, how would I deserialize it in the code behind?
EDIT: Sorry, this is in ASP.NET
.serializeArray() is for serializing form elements with name/value pairs, not a normal Array. To convert that to a string you want something like:
var serializedCheckData = checksSinceLastPostBack.join(',');
...or some other delimiter. If you have more complex data you may want to go a JSON route.