0

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.

4

3 に答える 3

1

You can encode your JavaScript object to JSON before sending it to server. Then you have to manually decode the json to use it as an array. In your ajax method do something like following.

   data : JSON.stringify(data),

EDIT:

JSON object is natively supported in newer browsers. For those that do not support it you can still achieve the functionality by including json2.js from http://json.org.

For more info on JSON visit https://developer.mozilla.org/En/Using_native_JSON

于 2012-04-26T13:26:38.630 に答える
1

Ommit the dataType: 'json' in an AJAX request, encode the data to a JSON within a jQuery and pass it to the PHP.

That should work.

于 2012-04-26T13:28:03.263 に答える
1

While you are accepting back JSON, you are sending normal form-encoded data (application/x-www-form-urlencoded):

arr%5B%5D=one&arr%5B%5D=two&obj%5Bone%5D=1&obj%5Btwo%5D=2

aka

arr[]=one&arr[]=two&obj[one]=1&obj[two]=2

You can confirm this from PHP by doing

$raw_input = file_get_contents("php://input");
echo urldecode($raw_input);

You are not sending {"arr":["one","two"],"obj":{"one":1,"two":2}} unless you explicitly convert in JavaScript, as the others have noted.

于 2012-04-26T13:42:43.807 に答える