Ben
Ben

Reputation: 25817

Json code retrieved with AJAX is unparsable

I'm a beginner.

I write simple json jquery php code to help understand the idea of json, but its not work, please help me.(I didnt write $_Post, submit, click function etc.. because its not working with, soo I cut the code to find the problem)

The user enters view.html and should get an alert box with value Bob - the problem is the user doesn't get the alert box.

view.html

<html>
<head>
<script type="text/javascript" 
        src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
</script>
<script type="text/javascript">
  $(document).ready(function () {
      $.ajax({
          url: 'controller.php',
          type: 'post',
          dataType: 'json',
          success: function (data) {
              alert(data["userdata"]["first"]);
          }
      });
  });​
</script>
</head>
<body>
</body>
</html>

controller.php:

<?php
  $arr=array();
  $arr['userdata']['first']='Dan';
  echo json_encode($arr);
?>

Thanks for help

Upvotes: 2

Views: 261

Answers (3)

Ben
Ben

Reputation: 25817

Thank to Jan I find the problem: my local zend server not worked (because i didnt define password for zend community server) when i fix it its starts working

Upvotes: 0

Aaron Butacov
Aaron Butacov

Reputation: 34367

Try adding the correct JSON header to your PHP file.

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

Upvotes: 1

Lizard
Lizard

Reputation: 45022

You will to check your apache's mod_security settings. This could be causing your 503 Service Temporarily Unavailable

Upvotes: 2

Related Questions