Advertisement

SACK AJAX with JSON

Moderators: Calcifer, slothy

SACK AJAX with JSON

Postby vernon » Fri Oct 20, 2006 8:35 am

I'm trying JSON but without success. In simple php script JSON working properly. Object values is show in alert box.
When i send by AJAX sack.php impementation result of $output = json_encode($val); in the javascript site is only string not object...
{"abc":12,"foo":"bar","bool0":false,"bool1":true,"arr":[1,2,3,null,5],"float":1.2345}

have you any suggession?
thanks
Vernon

PHP SCTIPT
<?php
$val = array("abc" => 12,
"foo" => "bar",
"bool0" => false,
"bool1" => true,
"arr" => array(1, 2, 3, null, 5),
"float" => 1.2345
);
$output = json_encode($val);

echo "<script>
var obj = { \"abc\": 12, \"foo\": \"bar\", \"bool0\": false, \"bool1\": true, \"arr\": [ 1, 2, 3, null, 5 ], \"float\": 1.234500 };
var result = obj[\"abc\"] * obj[\"float\"];
alert(\"result is \" + result);
</script>";
?>
vernon
 
Posts: 2
Joined: Fri Oct 20, 2006 8:24 am

Postby slothy » Fri Oct 20, 2006 12:46 pm

Hi Vernon

welcome to forums..

will have a look when i got time

Where you got json_encode from by the way?

*Edit *

Ah ok php function :)) sorry for some reason thought i was reading it as javascript!!



Alex
slothy
Moderator
 
Posts: 58
Joined: Fri Aug 25, 2006 1:42 pm

Javascript code for JSON object

Postby vernon » Mon Oct 23, 2006 8:23 am

function ajax_option_list_showContent(ajaxIndex,inputObj,paramToExternalFile){
var content = ajax_list_objects[ajaxIndex].response;
document.getElementById('loading').style.display= 'none';

var obj = new Object(content);
alert(obj); //not create OBJECT only string

//example create object
var obj = {"abc":12,"foo":"bar","bool0":false,"bool1":true,"arr":[1,2,3,null,5],"float":1.2345};
alert(obj);
var result = obj["abc"] * obj["float"]; /* obj.abc would be the same as obj["abc"] */
alert("result is " + result);


document.getElementById('a2').innerHTML = content;
}
vernon
 
Posts: 2
Joined: Fri Oct 20, 2006 8:24 am


Return to General questions regarding AJAX scripts

Who is online

Users browsing this forum: No registered users and 40 guests

cron