【问题标题】:AJAX success not executingAJAX 成功未执行
【发布时间】:2013-07-10 16:16:41
【问题描述】:

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

提前谢谢你!

【问题讨论】:

  • 浏览器控制台中的任何 500 个?
  • 在控制台或终端中没有
  • 您可以使用 Firebug 或开发工具之类的工具查看 200 响应吗?
  • 我在终端里看到了[10/Jul/2013 11:31:54] "POST /eventsearch/eventsearch/create/ HTTP/1.1" 200 16414
  • 你用的是什么 jQuery 版本?

标签: ajax django


【解决方案1】:

the jQuery.ajax docs 我可以读到这个:

弃用通知:jqXHR.success()、jqXHR.error() 和 jqXHR.complete() 回调自 jQuery 1.8 起已弃用。要为最终删除代码做好准备,请改用 jqXHR.done()、jqXHR.fail() 和 jqXHR.always()。

所以,我会按照那里的例子尝试一下是否可行:

// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.ajax( "example.php" )
    .done(function() { alert("success"); })
    .fail(function() { alert("error"); })
    .always(function() { alert("complete"); });
// perform other work here ...
// Set another completion function for the request above
jqxhr.always(function() { alert("second complete"); });

适应你的代码,它可能看起来像这样:

var jqxhr = $.ajax( "/eventsearch/eventsearch/createCustom/", {
     data: {name: name, loc: loc, start: start, end: end, tags: tags, event_id: event, profile: user}
    })
    .done(function() { alert("success"); })
    .fail(function() { alert("error"); })
    .always(function() { alert("complete"); });

我不完全确定数据,我只是在关注文档。

另外,最好在响应中添加mimetype,如下所示:

return HttpResponse('', mimetype="application/json")

我想我在某处读到如果没有使用 Django 设置 Ajax 将无法工作,我不记得在哪里,我试图在 Google 上找到它,试试看。

希望这会有所帮助!

【讨论】:

  • 绝对是在正确的轨道上! .fail 函数是始终执行的函数,但我的函数不再起作用。不太清楚为什么,因为终端显示 200。
  • 您应该将此与 nmware 的答案结合起来,我认为您需要将 content_type='application/json' 设置为 HttpResponse 才能使其工作。
  • @Xonal 我用一个可能有效的响应示例编辑了答案。
  • 尝试了 mimetype 和 content_type。都没有让它再次工作。
  • 这里一定是我们遗漏了一个愚蠢的错误。尝试发布完整视图的方法和您在浏览器中收到的完整响应。
【解决方案2】:

您需要在 anwer 中返回 - JSON
例如

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-17
    • 2016-04-20
    • 2015-06-13
    • 1970-01-01
    相关资源
    最近更新 更多