I'm pretty new to Ajax and Django and I'm trying to send a simple ajax request to a function called 'update'. But I also don't want the actual url to change in the browser when the request is sent (example www.website.com/page/ will stay the same even with an ajax request). Basically, when I try to submit the ajax request I get a 403 error from the server. I believe part of my problem could be the url mapping in urls.py...
This is my ajax request:
$.ajax({
type : "POST",
url : "/page/update/",
data : {
data : somedata,
},
}).done(function(data){
alert(data);
});
This is the view it should get:
def update(request):
if request.is_ajax():
message = "Yes, AJAX!"
else:
message = "Not Ajax"
return HttpResponse(message)
This is my urls.py
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^update/$', views.update, name='update'),
)
Thank you in advance for the help.
I looked a little more deeper into the error and the error states that I need to include {% csrf_token %} when sending a post back. This example how to get POST data in django 1.3 shows that its placed into a form however my request is only called on an on click function