1

I use jQuery's $.post to get data from php[from mysql] file data, what we get looks like that:

data = [['12-25-2012',62],['12-26-2012',60]]

when I assign this to variable it stores as string as this:

v1 = "[['12-25-2012',62],['12-26-2012',60]]"; [v1 = data]

but I require it to like this:

v2 = [['12-25-2012',62],['12-26-2012',60]];

not as a string.

To be more clarifying, you can understand by alert v1 & v2

when you alert v2 you get 12-25-2012,62,12-26-2012,60 when you alert v1 you get [['12-25-2012',62],['12-26-2012',60]]

there's difference, and I want v1 to look like v2.

Amyth
  • 32,527
  • 26
  • 93
  • 135
Az Wadehra
  • 23
  • 5

5 Answers5

1

Try to specify what type of data you expect to get in the jQuery $.post function according to the docs for $.post

Let php echo exactly this

$data = "[['12-25-2012',62],['12-26-2012',60]]";
exit(0);

and

$.post("test.php", { name: "John", time: "2pm" },
 function(data) {
   process(data);
 }, 
 "JSON" //this will tell your calling post that the return data is to be considered JSON
);
LoneWOLFs
  • 2,306
  • 3
  • 20
  • 38
0

Either use $.ajax and set JSON asthe the expected response type or parse the string

JSON.parse(v1);

As @m90 pointed out, single quotes aren't valid in JSON. I've missed that. You will have to change the php file to respond a proper JSON or replace the single quotes with double quotes before parsing the string.

Andreas
  • 21,535
  • 7
  • 47
  • 56
0

AJAX is only capable of transferring Strings, so you have to handle the conversion (to a String in PHP and back to the actual data in JS) yourself (this means that you cannot pass data like in your example: data = [['12-25-2012',62],['12-26-2012',60]]). The most common format used in AJAX calls is JSON. Yet, JSON is using double quotes only, so the data you are trying to pass in considered invalid.

I would advise you to use proper JSON formatting for your data (using double quotes) so that you can use JSON.parse for the job, but in case you are stuck with the formatting of your data you will have to get your hands dirty and use something like a function constructor (or, even worse use eval):

var v1 = "[['12-25-2012',62],['12-26-2012',60]]"
var a = new Function("return " + v1); //constructs a new function from the given string, in this case returning the passed array
var data = a();

console.log(data);

Read about using JSON in JavaScript at MDN and why you shouldn't be using eval and the likes.

PHP also has a very handy json_encode method that you can pass an Array and it will automatically be converted into valid JSON.

If you need just a simple String consisting of comma delimited pairs why don't you generate this in PHP and pass it to the JavaScript like this?

Community
  • 1
  • 1
m90
  • 11,434
  • 13
  • 62
  • 112
  • i tried to convert above to jason parse format but won't work i require that it get data from php that [['12-25-2012',62],['12-26-2012',60]] and it will show like this - 12-25-2012,62,12-26-2012,60 // – Az Wadehra Dec 25 '12 at 13:50
  • I think you need to specify on the desired output. Do you want a comma separated string and get rid of the Arrays? Do you want numbers to stay numbers, etc.? – m90 Dec 25 '12 at 13:53
  • k actually i generating graph using jqplot plugin and get data from database it require data in that format as shown in v2 variable but i get format as v1 i want to change v1 format as v2 u can go here to check out one example of jqplot format http://www.jqplot.com/docs/files/usage-txt.html – Az Wadehra Dec 25 '12 at 14:01
  • That would be pretty basic (and pretty unrelated to your actual question): http://jsfiddle.net/FPLcF/ – m90 Dec 25 '12 at 14:12
  • that won't work you can try yourself go here http://jsfiddle.net/45nDy/2/ you can change line1 to output then no graph generate when u use line1 it form graph – Az Wadehra Dec 25 '12 at 14:37
0

Maybe you can try this, though not sure whethe it would work:

v1=eval(data);

Please note that eval() is evil. Also I would strongly suggest, that you alter the php code to send it in some other format and then you could append these to a javascript array.

Community
  • 1
  • 1
gopi1410
  • 6,567
  • 9
  • 41
  • 75
0

use this ..

    var x= eval(v1);
     alert(x);
sourcecode
  • 1,802
  • 2
  • 15
  • 17