I guess this is the opposite of normal case. I wish to receive the data in PHP as String with JSON in it, and I wish jQuery to Encode the data.
currently PHP decodes automatically the data to an array.
PHP 5.3.10 ( hosted ).
<?php
if( isset( $_REQUEST['arr']))
{
$arr = $_REQUEST['arr'];
$obj = $_REQUEST['obj'];
$res = "arr is of type ".gettype( $arr).", var_export (".var_export( $arr, true).")\n"
. "obj is of type ".gettype( $obj).", var_export (".var_export( $obj, true).")\n";
die( json_encode( $res ));
exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" ></script>
<script>
$(document).ready(function(){
$("#csubmit").click( function() {
var arr = new Array("one","two");
var obj = { "one":1, "two":2 };
var data ={ "arr" :arr, "obj" : obj };
$.ajax({
type: "POST",
cache: false,
data: data,
dataType : "json",
complete : function( jqXHR, textStatus ) {
if( textStatus != 'success' )
alert("Network error ("+textStatus+"/"+jqXHR.responseText+")");
}
}).done( function( data ) {
alert("Got back:("+data+")");
});
return false;
});
});
</script>
</head>
<body>
<form><input type="submit" value="click" id="csubmit"></form>
</body>
</html>
Result is always
Got back:(arr is of type array, var_export (array (
0 => 'one',
1 => 'two',
))
obj is of type array, var_export (array (
'one' => '1',
'two' => '2',
))
)
even though I'd like it to be a few strings.