how to send Array value from server?
If I send it like this:
"a","b","c"
then in JavaScript (after GET and Alert) I have only "a", for example:
$.get('xx', function(labels){
alert(labels); // <- here I have only "a"
});
how to receive all 3 values from server?
Thanks
OK it is solved using:
JSON.parse(labels)
thanks to everybody!
If you want it to be done using pure javascript,you can do this.
Since you have not mentioned which server side language you using,I will show with JSP.
First store your array in a javascript variable.
var x=new Array();
x="<%=Serverarray%>"
This will give an output like this "[a,b,c]"
In order to retrieve values from the database you need to use x[0],x[1].....
x[0]=a;
x[1]=b;
x[2]=c;
Related
I have an array of JSON plots which I store in MySQL. When I retrieve this information from MySQL it is given as one long string. How can I restore this back into an array of JSON objects using Javascript? I'm running this using NodeJS and MySQL package.
My data is returned like the following:
'[{"x":0,"y":0},{"x":1,y:1},{"x":2,"y":2}]'
What I would like to be able to do is use the data like:
var data = [{"x":0,"y":0},{"x":1,"y":1},{"x":2,"y":2}];
console.log(data[0].x);
I've had a try using JSON.parse and originally stored the data using JSON.stringify on the array, but it is not behaving as I would expect.
Are there any methods or packages available to handle this?
Edit: I realize now that this is not JSON but rather objects. Apologies for the wrong terminology here, but my problem still remains.
var data = new Function ('return ' + dataString)();
How to retrieve the session values which are stored in the list using javascript?
I used this code to set the session in the controller.
List<test> _test= new List<test>();
if (Session["testsession"] == null)
Session["testsession"] = _test;
I used this code to retrieve the session list values using javascript
var TEST ='#HttpContext.Current.Session["quotesession"]';
but when i debug it the output is in
var TEST ='System.Collections.Generic.List`1[NLG.IMS.Shared.Models.Test]';
Where i went wrong? I need the session values to be retrieved from the list.
I would advise using a strongly typed model and assigning a property to the collection rather than using session, even the ViewBag would be better but if you really must, this is how you could:
You could use the following:
var json = #Html.Raw(Json.Encode(#HttpContext.Current.Session["quotesession"]));
Which would output it as json.
jsFiddle
The above prints out a collection to the console.log.
I need to transfer a multi-dimensional JavaScript array to another page, without using any library. What I can use is JavaScript, PHP and other languages that doesn't need a library.
I have a three-dimensional array which is build like this:
storage[category][field][multiple answers] and has a lot of values.
I need to transfer it to another page so I can get all the values like:
alert(storage[5][4][8]);
=======================================================================
Well, I can pass a normal variable to another page but I cant get the values from my array when I'm testing: storage[1][1][1] for example.The big question is how I can pass a multidimensional array to another page and still be able to get the values like this: storage[1][1][1]
As I get it I'm forced to pass all the 121 arrays you can se below to be able to access all dimensions in the array.
My array is built up like this:
storage = new Array();
for (var i1=1;i1<12;i1++){
storage[i1] = new Array();
for (var i2=1;i2<12;i2++){
storage[i1][i2] = new Array();
}
}
Without using a library like jQuery, you can convert your array to JSON, pass it via a URL and decode it on the target page. Converting it to JSON would look like:
var json_string = JSON.stringify(your_array);
Then pass it in a URL:
var your_url = "http://www.your_website.com/page.html?json_string=" + json_string;
And you could decode it back to an array like so:
var your_new_array = JSON.parse(getUrlVars()["json_string"]);
For some more reading, check out this JSON page: http://www.json.org/js.html
JSON.stringify() is supported by all major browsers. Send it to the server via a POST, then have your php retrieve the variable from $_POST and send it back.
As far as I can see there are two main ways to do what you want:
Pass the array to the webserver, and have it send it back on next request.
Store the data locally in the browser.
The first way could get pretty complicated. You would have to store the data in a database, file or cookie/session.
The second way would be the easiest. Use javascript to store the array in the browser. You can either use a cookie, or use the localStorage object.
Using a cookie would require you to serialize the data manually. It would also get passed to the server, so if you want to save bandwidth, you would want to avoid this.
The localStorage method would only store the data locally, and you also don't need to serialize anything, the browser takes care of this for you.
See the links below for more examples.
http://www.w3schools.com/html/html5_webstorage.asp
http://www.w3schools.com/js/js_cookies.asp
Ok, I know there were many of those but my code just won't work... I tried solutions found on Stack but still...
I would like to transfer my array built in jQuery to my php script that will compare it with other array etc.
My jQuery code looks like this:
$.post('rezultat.php',{wyb: JSON.stringify(wybrane)});
and in my rezultat.php file I try to decode it:
$wyb = json_decode($_POST['wybrane']);
I tried the $_POST method before, with no results. What am I doing wrong?
You need to use $_POST['wyb'] instead of $_POST['wybrane'] in your php script, since you used 'wyb' key in the array you created in javascript.
You are calling the parameter with his Javascript name, try to call with the $_POST name:
$wyb = json_decode($_POST['wyb']);
I currently have the following javascript array:
var stuffs = ['a', 'b'];
I pass the above to the server code using jQuery's load:
var data = {
'stuffs': stuffs
};
$(".output").load("/my-server-code/", data, function() {
});
On the server side, if I print the content of request.POST(I'm currently using Django), I get:
'stuffs[]': [u'a', u'b']
Notice the [] at the prefix of the variable name stuffs. Is there a way to remove that [] before it reaches the server code?
This is default behavior in jQuery 1.4+...if you want the post to be &stuffs=a&stuffs=b instead of &stuffs[]=a&stuffs[]=b you should set the traditional option to true, like this:
$.ajaxSetup({traditional: true});
Note this affects all requests... which is usually what you want in this case. If you want it to be per-request you should use the longer $.ajax() call and set traditional: true there. You can find more info about traditional in the $.param() documentation.
When an array is submitted using a GET request, through a form or AJAX, each element is given the name of the array, followed by a pair of optionally empty square brackets. So the jQuery is generating the url http://example.com/get.php?stuff[]=a&stuff[]=b. This is the only way of submitting an array, and the javascript is following the standard.
POST requests work in exactly the same way (unless the json is sent as one long json string).
In PHP, this is parsed back into the original array, so although the query string can be a little strange, the data is recieved as it was sent. $_GET['stuff'][0] works correctly in PHP.
I'm not sure how Django parses query strings.
The [] indicates that the variable is an array. I imagine that the appending of the [] to your variable name is Python/Django's way of telling you it is an array. You could probably implement your own print function which does not show them.