我有一个使用 ajax 提交的表单。提交总是成功的,但我的成功声明中没有任何内容执行过。我有我的 django 视图返回 a status = 200
,但仍然没有。谁能告诉为什么这没有执行?在我的终端中,它显示我有一个损坏的管道错误,但我读过我可以忽略我的开发服务器上的那些。不确定这是否与它有关。
我的表格:
<form onsubmit="createAdd({{ newID }}, {{ user.get_profile.id }})" class="form-horizontal create" method="post">{% csrf_token %}
<div class="control-group">
<label class="control-label" for="newName">Name*</label>
<div class="controls">
<input type="text" name="newName" id="name" required>
</div>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" class="btn btn-primary" value="Create"><i class="icon-wrench icon-white"> </i> Create </button>
</div>
</div>
</form>
我的 AJAX 功能:
function createAdd(event, user){ // Creates a custom event and automatically gives creator ownership
var name = document.getElementById('name').value;
var loc = document.getElementById('loc').value;
var start = document.getElementById('datepicker').value;
var end = document.getElementById('datepicker2').value;
var tags = document.getElementById('tags').value;
var jqxhr = $.ajax( "/eventsearch/eventsearch/createCustom/", {
type: "POST",
data: {name: name, loc: loc, start: start, end: end, tags: tags, event_id: event, profile: user}
})
.done(function() { alert("success"); })
.fail(function() { alert("hello") })
}
我实际上并不想去谷歌,我只是想让它重定向到某个地方。如果我把一个也放在那里,它也不会执行alert
。
我的全貌:
@login_required
def createCustom(request):
newID = len(customEvent.objects.all())
newName = request.POST['name']
newLoc = request.POST['loc']
newStart = request.POST['start']
newEnd = request.POST['end']
newTags = request.POST['tags']
newURL = "/eventc/" + str(newID)
e = customEvent(event_id = newID, title = newName, start = newStart, end = newEnd, location = newLoc, tags = newTags, url = newURL)
e.save()
event_id = request.POST['event_id']
user = request.POST['profile']
event = customEvent.objects.get(event_id = event_id)
user = Profile.objects.get(id = user)
user.ownedEvent.add(event)
return HttpResponse('', content_type="application/json")
先感谢您!