Skip to content

Instantly share code, notes, and snippets.

@junpeitsuji
Created January 13, 2013 17:12
Show Gist options
  • Save junpeitsuji/4525118 to your computer and use it in GitHub Desktop.
Save junpeitsuji/4525118 to your computer and use it in GitHub Desktop.
Sample code to send a JSON message via POST using jQuery.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>POST TEXT HTML</title>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
// <!--
$(function(){
$("#post button").click( function(){
var url = 'posttest.php';
var jsondata = {
id: $("#post input[name='id']").val(),
name: $("#post input[name='name']").val()
};
alert(JSON.stringify(jsondata));
$.post(url,
jsondata,
function (result) {
$("#post textarea").text(''+JSON.stringify(result)+'');
},
"json"
);
})
})
// -->
</script>
</head>
<body>
<div id="header">
<h1>POST TEST</h1>
</div>
<div id="post">
<p>id: <input type="text" name="id" />
name: <input type="text" name="name" />
<button type="button">submit</button></p>
<p>response:</p>
<textarea cols=150 rows=8 disabled ></textarea>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment