【问题标题】:Ajax success, complete, error function is not calling jqueryAjax成功,完成,错误函数是没有调用jquery
【发布时间】:2016-07-06 09:37:05
【问题描述】:

我对 jQuery Ajax 调用方法成功有疑问。我尝试了一切,但没有成功。我的代码看起来像

$('#target').submit(function(event) {
    // get the form data
    var formData = {
        'name': $('input[name=name]').val(),
        'surname': $('input[surname=surname]').val(),
    };

    // process the form
    $.nette.ajax({
        type: 'POST', 
        url: {link Admin:uploadData}, 
        data: formData, // our data object
        dataType: 'json',
        async: false,
        contentType: "application/json",
        off: ['unique'],
        success: function(payload){
            console.log(payload.message.lol);
        }
    })
    event.preventDefault();
});

我正在使用 nette 网络框架。当我在 Firefox 中调试代码时,我得到如下响应:

感谢您的建议。

【问题讨论】:

  • 我认为$('input[surname=surname]').val() 应该是$('input[name=surname]').val(),同时删除async: false。这是一个非常糟糕的主意。
  • 什么是off属性?
  • 我不知道,我只是在 nette.org 论坛上找到的。我第一次使用 Ajax,所以我不知道它在做什么。但是当我删除行时......它也不起作用。
  • 不要只是复制和粘贴您在互联网上看到的所有内容,而不知道它的作用,哈哈
  • 是的,我知道,但我不知道如何解决这个问题。

标签: jquery ajax nette


【解决方案1】:

你甚至不需要 nette.ajax,简单的 jQuery.ajax 就足够了。

$('#target').submit(function(event) {
    // get the form data
    var formData = {
        'name': $('input[name=name]').val(),
        'surname': $('input[name=surname]').val(),
    };

    // process the form
    $.ajax({
        type: 'POST',
        url: $(this).attr('action'), // assuming #target is a form, we can use its action
        data: formData, // our data object
        dataType: 'json',
        contentType: 'application/json',
        success: function(payload) {
            console.log(payload.message.lol);
        }
    });

    event.preventDefault();
});

另外,正如 @Rory McCrossan 评论的那样,async 属性只会让您的网站冻结,除非您真的知道自己需要它,否则不要使用它。

【讨论】:

  • 感谢这个网址,但是当我使用 jQuery.ajax 时,我得到的答案是整个 HTML 页面,没有 JSON 数据。
  • jQuery 应该添加 X-Requested-With: XMLHttpRequest 标头,您可以在演示者中使用 isAjax 方法检测到并在这种情况下发送 JSON 有效负载。
  • 是的,我知道,但它不起作用。我总是得到整个 html 页面的响应。
猜你喜欢
  • 2013-01-13
  • 2015-07-12
  • 2014-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-14
  • 1970-01-01
相关资源
最近更新 更多