Skip to content

Instantly share code, notes, and snippets.

@chrismeller
Created September 18, 2011 16:26
Show Gist options
  • Save chrismeller/1225235 to your computer and use it in GitHub Desktop.
Save chrismeller/1225235 to your computer and use it in GitHub Desktop.
<?php
if ( !isset( $_GET['type'] ) ) {
$type = 'json';
}
else {
$type = $_GET['type'];
}
switch ( $type ) {
case 'json':
header( 'Content-Type: application/json' );
break;
case 'javascript':
header( 'Content-Type: text/javascript' );
break;
case 'javascript-utf8':
header( 'Content-Type: text/javascript;charset=utf-8' );
break;
case 'plain':
default:
header( 'Content-Type: text/plain' );
break;
}
$content = array( 0, 1, 2, 3 );
echo json_encode( $content );
?>
<html>
<head>
<title>JSON Test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready( function () {
$('#go').click( function () {
var type = $('#type').val();
$.get( $(this).attr('href'), {type: type}, function ( data ) {
$(data).each( function ( index ) {
$('#response').append( '<li>' + this + '</li>' );
} );
} );
return false;
} );
} );
</script>
</head>
<body>
<select id="type">
<option value="json">application/json</option>
<option value="javascript">text/javascript</option>
<option value="javascript-utf8">text/javascript;charset=utf8</option>
<option value="plain">text/plain</option>
</select>
<a href="json_back.php" id="go">Run</a>
<ul id="response"></ul>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment