Note that there are some explanatory texts on larger screens.

plurals
  1. POReceiving jQuery JSON data as JSON in PHP
    primarykey
    data
    text
    <p>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.</p> <p>currently PHP decodes automatically the data to an array.</p> <p>PHP 5.3.10 ( hosted ).</p> <pre><code>&lt;?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; } ?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt; &lt;title&gt;test&lt;/title&gt; &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" &gt;&lt;/script&gt; &lt;script&gt; $(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; }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form&gt;&lt;input type="submit" value="click" id="csubmit"&gt;&lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Result is always</p> <pre><code>Got back:(arr is of type array, var_export (array ( 0 =&gt; 'one', 1 =&gt; 'two', )) obj is of type array, var_export (array ( 'one' =&gt; '1', 'two' =&gt; '2', )) ) </code></pre> <p>even though I'd like it to be a few strings.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload