gpt4 book ai didi

AJAX成功不执行

转载 作者:行者123 更新时间:2023-12-01 11:43:15 26 4
gpt4 key购买 nike

我有一个使用 ajax 提交的表单。提交总是成功的,但我的成功声明中的任何内容都没有执行。我让我的 Django View 返回一个 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") })
}

我实际上并不想访问 Google,我只是想让它重定向到某个地方。如果我在其中放置一个 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")

提前致谢!

最佳答案

您需要返回答案 - JSON
例如

def answer(request):    
# same you code
payload = {'success': True}
return HttpResponse(json.dumps(payload), content_type='application/json')

关于AJAX成功不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17575803/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com