I'm not explicitly setting a content-type, but Firebug tells me it's
text/html .  I'll try changing it to application/json.

Erm... I must be doing something else wrong in my script.  Just now I
just set up a very simple test using both regular  $.ajax()   and
using  jquery.form.js's  $.ajaxForm()  and both work correctly.

If anyone's interested, here are both test scripts:

bool_json_test.html:

<!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>AJAX JSON Test</title>

<script type="text/javascript" src="/scripts/jquery/jquery.js"></
script>
<script type="text/javascript" src="/scripts/jquery/jquery.form.js"></
script>
</head>

<body>
<form id="no_submit" name="no_submit" method="get" action="">
  <p>Return type:  </p>
  <blockquote>
    <label>
      <input type="radio" name="datatype" value="string"
id="datatype_0" />
    String</label>
    <br />
    <label>
      <input type="radio" name="datatype" value="integer"
id="datatype_1" />
    Integer</label>
    <br />
    <label>
      <input type="radio" name="datatype" value="float"
id="datatype_2" />
    Float</label>
    <br />
    <label>
      <input type="radio" name="datatype" value="boolean"
id="datatype_3" />
    Boolean</label>
    <br />
    <label>
      <input type="radio" name="datatype" value="null"
id="datatype_4" />
    Null</label>
    <br />
  </blockquote>
  <p>

    <input type="button" name="click_me" id="click_me" value="Click
Me" />
  </p>
</form>
<table border="1" cellspacing="0" cellpadding="2">
  <tr>
    <td>Data type returned:</td>
    <td id="data_type">&nbsp;</td>
  </tr>
  <tr>
    <td>Value returned:</td>
    <td id="value">&nbsp;</td>
  </tr>
</table>
<script type="text/ecmascript">
$(function()  {
        $("#click_me").click(function()  {
                $.ajax({
                        url      : 'bool_json_test.ajax.php',
                        dataType : 'json',
                        type     : 'GET',
                        data     : {
                                type    : $("[name=datatype]:checked").val()
                        },
                        success  : function(json)  {
                                var text = (json===null) ? "null" : 
json.toString();
                                $("#data_type").text(typeof json);
                                $("#value").text("");
                                $("#value").text(text);
                        }
                });
        });
});
</script>
<form id="ajax_form" name="ajax_form" method="get"
action="bool_json_test.ajax.php">
  <p>Return type: </p>
  <blockquote>
    <label>
      <input type="radio" name="datatype" value="string"
id="datatype_5" />
      String</label>
    <br />
    <label>
      <input type="radio" name="datatype" value="integer"
id="datatype_6" />
      Integer</label>
    <br />
    <label>
      <input type="radio" name="datatype" value="float"
id="datatype_7" />
      Float</label>
    <br />
    <label>
      <input type="radio" name="datatype" value="boolean"
id="datatype_8" />
      Boolean</label>
    <br />
    <label>
      <input type="radio" name="datatype" value="null"
id="datatype_9" />
      Null</label>
    <br />
  </blockquote>
  <p>
    <input type="submit" name="click_me2" id="click_me2" value="Click
Me" />
  </p>
</form>
<table border="1" cellspacing="0" cellpadding="2">
  <tr>
    <td>Data type returned:</td>
    <td id="data_type2">&nbsp;</td>
  </tr>
  <tr>
    <td>Value returned:</td>
    <td id="value2">&nbsp;</td>
  </tr>
</table>
<script type="text/ecmascript">
$(function()  {
        $("#ajax_form").ajaxForm({
                dataType : 'json',
                success  : function(json)  {
                        var text = (json===null) ? "null" : json.toString();
                        $("#data_type2").text(typeof json);
                        $("#value2").text("");
                        $("#value2").text(text);
                }
        });
});
</script>
</body>
</html>


bool_json_test.ajax.php:

<?php
$type = @$_GET['type'];
if (!$type) { $type = $_GET['datatype']; }
$rets = array(
        'string'    => "Hello World",
        'integer'   => 5,
        'float'     => 10.3,
        'boolean'   => true,
        'null'      => NULL
);

print json_encode($rets[$type]);
?>


Thanks,
Jamie

On Nov 19, 10:19 pm, Michael Geary <m...@mg.to> wrote:
> You would normally expect that to work.
>
> What content-type is your server putting in the header for the JSON data?
> That could be throwing it off.
>
> Also note that a bare primitive value (true, false, null, or a string or
> number) is not valid JSON. The only valid JSON is either an object enclosed
> in {} or an array enclosed in []. However, this is not what's causing your
> problem. jQuery doesn't use a strict JSON parser - it simply evals the JSON
> text - so a bare primitive value should work fine if everything else is OK.
>
> -Mike
>
> On Thu, Nov 19, 2009 at 11:08 AM, livefree75 <jpittm...@gmail.com> wrote:
> > Hi,
>
> > I'm using the following code on the client side:
> >   $.ajax({
> >      dataType : 'json',
> >      // other options
> >      success : function(json_response)  {
> >         console.log(typeof response, response);   // Using Firefox's
> > firebug
> >      }
> >   });
>
> > And this PHP code on the server side:
>
> > <?php
> >   // php processing code
> >   $response = some_boolean_value();
> >   print json_encode($response);
> > ?>
>
> > Now, using Firebug, I can verify that the actual JSON response coming
> > back is indeed either   true   or   false   (without the quotes),
> > which should evaluate to Javascript boolean true or false.  However,
> > when I obtain it in the success() method of my $.ajax() call, it comes
> > in as a string.  (e.g.  "true"  or  "false").  i.e., the console.log()
> > call renders:   string true
>
> > Shouldn't it render:   boolean true  ?
>
> > Is this a bug?
> > Jamie

Reply via email to