【问题标题】:Jquery ajax abort() gives error on console [duplicate]Jquery ajax abort()在控制台上给出错误[重复]
【发布时间】:2016-02-23 22:49:40
【问题描述】:

我的 jQuery 代码(如下)方法给出以下错误:

TypeError: jQuery111007245809631238611_1456231588251 不是函数

    $(function() {
      // Ajax request sent.
      var xhr = $.ajax({
        url: 'http://gdata.youtube.com/feeds/api/videos/wbB3lVyUvAM?v=2&alt=jsonc',
        data: {
          format: 'json'
        },
        beforeSend: function() {

        },
        dataType: 'jsonp',
        success: function(data) {
          //console.log(data);
          $('#info').html(data.error.message);
        },
        type: 'GET',
        error: function() {
          if (xhr.statusText == 'abort') {
            $('#info').html('Aborted!');
            return;
          }
          alert('error');
        }
      });
      // Abort ajax request on click


      $('#btn').on('click', function() {

        xhr.abort();
      });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>

<button id="btn">click to abort </button>

  <div id="info"></div>
</body>

(也可在http://jsfiddle.net/e3ok3s6e/3/ 获得)

有没有办法解决这个错误?

【问题讨论】:

  • 我没有收到您上面提到的错误。
  • 打开控制台,点击jsfiddle中的'click to abort'按钮,同时出现loading..。不是在数据加载之后。
  • 要获得该错误,必须非常快速地单击中止按钮。
  • 是的,但这不应该发生。我处于服务缓慢的情况,用户可以选择取消请求。 abort 方法工作正常,但是一旦我们从中止的请求中得到响应,就会出现错误。
  • 谢谢...会调查一下...

标签: jquery ajax


【解决方案1】:

你必须使用

dataType:'json' 在您的请求中..

    $(function() {
      // Ajax request sent.
      var xhr = $.ajax({
        url: 'http://gdata.youtube.com/feeds/api/videos/wbB3lVyUvAM?v=2&alt=jsonc',
        data: {
         dataType:'json'
        },
        beforeSend: function() {

        },
        dataType: 'jsonp',
        success: function(data) {
          //console.log(data);
          $('#info').html(data.error.message);
        },
        type: 'GET',
        error: function() {
          if (xhr.statusText == 'abort') {
            $('#info').html('Aborted!');
            return;
          }
          alert('error');
        }
      });
      // Abort ajax request on click


      $('#btn').on('click', function() {

        xhr.abort();
      });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>
  <div id="info"></div>
</body>

【讨论】:

  • 你能解释一下为什么或如何解决这个问题吗? JSONP 请求根本无法中止(您只能删除回调函数),所以我不确定这个答案如何解决问题。
  • 使用dataType:'json',这种情况下不支持,可以在控制台查看响应。
  • 由于我们无法中止 jsonp 请求,这个插件将解决这个问题:github.com/jaubourg/jquery-jsonp
猜你喜欢
  • 2015-08-27
  • 1970-01-01
  • 2015-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多