Created
January 13, 2013 17:12
-
-
Save junpeitsuji/4525118 to your computer and use it in GitHub Desktop.
Sample code to send a JSON message via POST using jQuery.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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