Skip to content

Instantly share code, notes, and snippets.

@dfeeney
Created July 8, 2011 22:33
Show Gist options
  • Save dfeeney/1072984 to your computer and use it in GitHub Desktop.
Save dfeeney/1072984 to your computer and use it in GitHub Desktop.
Ajax/django auth post tests
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="http://github.com/malsup/form/raw/master/jquery.form.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#ajaxformpost").ajaxForm({
target: "#ajaxformresult",
error: function(data) {
$("#rawpostresult").empty();
$("#rawpostresult").append("<div>Error: " + data + "</div>");
}
});
$("#rawpostsubmit").click(function() {
$.ajax({
type: "POST",
contentType: "text/plain",
data: $("#rawposttextarea").val(),
url: "http://localhost:8000/target/rawpost/",
success: function(data) {
$("#rawpostresult").empty();
$("#rawpostresult").append(data);
},
error: function(data) {
$("#rawpostresult").empty();
$("#rawpostresult").append("<div>Error: " + data + "</div>");
},
beforeSend: function(xhr){
xhr.withCredentials = true;
}
});
return false;
});
});
</script>
</head>
<body>
<h1>Remote post session test</h1>
<div>
<h2>Direct Form Post</h2>
<form id="formpost" action="http://localhost:8000/target/formpost/" method="post">
<textarea name="body"></textarea>
<input type="submit" value="Submit"/>
</form>
</div>
<div>
<h2>Ajax Form Post</h2>
<form id="ajaxformpost" action="http://localhost:8000/target/formpost/" method="post">
<textarea name="body"></textarea>
<input type="submit" value="Submit"/>
</form>
<b>Result:</b>
<div id="ajaxformresult"></div>
</div>
<div>
<h2>Raw body ajax post</h2>
<div>
<textarea id="rawposttextarea" name="body"></textarea>
<input id="rawpostsubmit" type="button" value="Submit"/>
</div>
<b>Result:</b>
<div id="rawpostresult"></div>
</div>
</body>
</html>
from django.conf.urls.defaults import patterns, url
from target import views
from django.views.decorators.csrf import csrf_exempt
urlpatterns = patterns('',
url(r'^rawpost/', csrf_exempt(views.RawPostTargetView.as_view()), name='raw_post'),
url(r'^formpost/', csrf_exempt(views.FormPostTargetView.as_view()), name='form_post')
)
from django.views.generic.base import View
from django.utils.decorators import method_decorator
from django.http import HttpResponse
class FormPostTargetView(View):
def post(self, request, *args, **kwargs):
body = request.POST["body"]
username = "Anonymous"
if request.user.is_authenticated():
username = request.user.username
resp = HttpResponse("<div><b>Posted by:</b>%s</div><div><b>Body:</b>%s</div>"%(username, body))
resp['Access-Control-Allow-Origin'] = 'http://localhost'
resp['Access-Control-Allow-Methods'] = 'POST'
resp['Access-Control-Allow-Headers'] = '*'
resp['Access-Control-Allow-Credentials'] = 'true'
return resp
class RawPostTargetView(View):
def post(self, request, *args, **kwargs):
body = request.raw_post_data
username = "Anonymous"
if request.user.is_authenticated():
username = request.user.username
resp = HttpResponse("<div><b>Posted by:</b>%s</div><div><b>Body:</b>%s</div>"%(username, body))
resp['Access-Control-Allow-Origin'] = 'http://localhost'
resp['Access-Control-Allow-Methods'] = 'POST'
resp['Access-Control-Allow-Headers'] = '*'
resp['Access-Control-Allow-Credentials'] = 'true'
return resp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment