JQuery AJAX post方法麻烦

时间:2013-04-01 03:20:09

标签: jquery ajax

我无法弄清楚这个AJAX帖子有什么问题。数据被发布到服务器并由数据库记录,但是我得到了一个无法解释的错误消息,而不是成功的重定向。我应该从这个错误消息中拿走什么来解决为什么我没有成功重定向?

编辑:我还应该注意,POST在“开发人员工具网络”标签中处于“待处理”状态。

代码:

$('#submit').click(function(e) {

    data = {
      vimeoVideoId : '22077152'
    };

    console.dir(data);

    var url = '/videos/new-entry';

    $.ajax({
      type: 'POST',
      url: url,
      data: data,
      dataType: 'json',
      success: function(response) {
        console.log('response.status == "OK"');
        if (response.status == "OK") {
          window.location.href = '/videos/' + '22077152';
        }
      },
      error : function(error) {
        console.dir(error);
      }
    });
  e.preventDefault();
  return false;
});

错误讯息:

Object {readyState: 4, getResponseHeader: function, getAllResponseHeaders: function, setRequestHeader: function, overrideMimeType: function…}
abort: function (e){var t=e||T;return l&&l.abort(t),k(0,t),this}
always: function (){return i.done(arguments).fail(arguments),this}
complete: function (){if(u){var t=u.length;(function i(t){b.each(t,function(t,n){var r=b.type(n);"function"===r?e.unique&&p.has(n)||u.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=u.length:r&&(s=t,c(r))}return this}
done: function (){if(u){var t=u.length;(function i(t){b.each(t,function(t,n){var r=b.type(n);"function"===r?e.unique&&p.has(n)||u.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=u.length:r&&(s=t,c(r))}return this}
error: function (){if(u){var t=u.length;(function i(t){b.each(t,function(t,n){var r=b.type(n);"function"===r?e.unique&&p.has(n)||u.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=u.length:r&&(s=t,c(r))}return this}
fail: function (){if(u){var t=u.length;(function i(t){b.each(t,function(t,n){var r=b.type(n);"function"===r?e.unique&&p.has(n)||u.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=u.length:r&&(s=t,c(r))}return this}
getAllResponseHeaders: function (){return 2===x?a:null}
getResponseHeader: function (e){var t;if(2===x){if(!c){c={};while(t=Tn.exec(a))c[t[1].toLowerCase()]=t[2]}t=c[e.toLowerCase()]}return null==t?null:t}
overrideMimeType: function (e){return x||(p.mimeType=e),this}
pipe: function (){var e=arguments;return b.Deferred(function(n){b.each(t,function(t,o){var a=o[0],s=b.isFunction(e[t])&&e[t];i[o[1]](function(){var e=s&&s.apply(this,arguments);e&&b.isFunction(e.promise)?e.promise().done(n.resolve).fail(n.reject).progress(n.notify):n[a+"With"](this===r?n.promise():this,s?[e]:arguments)})}),e=null}).promise()}
progress: function (){if(u){var t=u.length;(function i(t){b.each(t,function(t,n){var r=b.type(n);"function"===r?e.unique&&p.has(n)||u.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=u.length:r&&(s=t,c(r))}return this}
promise: function (e){return null!=e?b.extend(e,r):r}
readyState: 4
responseText: "<!DOCTYPE html><html><head><link rel="stylesheet" href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css"><link rel="stylesheet" href="/stylesheets/flat-ui.css"><link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/3.0.2/css/font-awesome.css"><link rel="stylesheet" href="/stylesheets/unislider/dpUniSlider.css"><link rel="stylesheet" href="/stylesheets/styles.css"><link rel="stylesheet" href="/stylesheets/D3.css"><script type="text/javascript" src="http://d3js.org/d3.v3.min.js"></script></head><body><div class="topbar navigation"><div class="fill"><div class="navbar navbar-inverse"><div class="navbar-inner"><div class="container"><ul class="nav"><li><a href="/videos/">Notestream</a></li></ul></div></div></div></div></div><div class="container-fluid"><section class="row-fluid"><div class="span6"><h2 id="video-title"></h2><p id="video-id" class="video-info">5158f3c7fd03dda7f6000002</p><p id="vimeoVideoId" class="video-info">22077152</p><a href="/videos/22077152/destroy">Delete</a></div><div class="span6"><br><div class="btn-group"><button id="your-notes" class="btn">Your Notes</button><button id="all-notes" class="btn">All Notes</button></div></div></section><br><section class="row-fluid"><div id="add-video" class="span6"><iframe id="player" src="http://player.vimeo.com/video/22077152?api=1&amp;player_id=player" width="540" height="304" frameborder="0" webkitallowfullscreen="webkitallowfullscreen" allowfullscreen="allowfullscreen"></iframe></div><div class="span4 note-edit-view"><h4>Your Recent Notes</h4><div id="notes-append"></div></div><div class="span6 unislider-view"><div id="chartSet"><script type="text/javascript" async src="/js/D3/chart.js"></script></div></div></section><br><section class="row-fluid notes-edit-view"><div class="span5 note-edit-view"><form id="noteForm" method="POST" action="/videos/5158f3c7fd03dda7f6000002/note"><fieldset><textarea id="noteText" name="noteText" rows="6" cols="50" placeholder="Add a note" autofocus class="input-xlarge"></textarea><input id="timecode" type="hidden" name="timecode" value="timecode"><input type="hidden" name="videoId" value="5158f3c7fd03dda7f6000002"><input type="hidden" name="author" value="ee"><br><button id="noteButton" type="button" class="btn btn-large">Enter</button></fieldset></form></div></section><section class="row-fluid note-edit-view"><br><a href="/videos/22077152/csv">Download notes</a><br><br></section></div><div class="container-fluid unislider-view"><h4>All Notes</h4><div class="unslider-bkgr">&nbsp;<ul id="unislider"></ul></div></div><script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script><script type="text/javascript" src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script><script type="text/javascript" src="/js/unislider/jquery.dpUniSlider.min.js"></script><script type="text/javascript" src="http://a.vimeocdn.com/js/froogaloop2.min.js?8a15a-1360785454"></script><script type="text/javascript" src="/js/notestreamjs/notestream-show.js"></script><script type="text/javascript" src="//use.typekit.net/hbs1mpa.js"></script><script type="text/javascript">try{Typekit.load();}catch(e){}↵</script></body></html>"
setRequestHeader: function (e,t){var n=e.toLowerCase();return x||(e=v[n]=v[n]||e,y[e]=t),this}
state: function (){return n}
status: 200
statusCode: function (e){var t;if(e)if(2>x)for(t in e)m[t]=[m[t],e[t]];else N.always(e[N.status]);return this}
statusText: "OK"
success: function (){if(u){var t=u.length;(function i(t){b.each(t,function(t,n){var r=b.type(n);"function"===r?e.unique&&p.has(n)||u.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=u.length:r&&(s=t,c(r))}return this}
arguments: null
caller: null
length: 0
name: ""
prototype: Object
__proto__: function Empty() {}
<function scope>
then: function (){var e=arguments;return b.Deferred(function(n){b.each(t,function(t,o){var a=o[0],s=b.isFunction(e[t])&&e[t];i[o[1]](function(){var e=s&&s.apply(this,arguments);e&&b.isFunction(e.promise)?e.promise().done(n.resolve).fail(n.reject).progress(n.notify):n[a+"With"](this===r?n.promise():this,s?[e]:arguments)})}),e=null}).promise()}
__proto__: Object

4 个答案:

答案 0 :(得分:0)

变化:

success: function(response) {
        console.log('response.status == "OK"');
        if (response.status == "OK") {
          window.location.href = '/videos/' + '22077152';
        }
      }

....
success: function(response,  textStatus, xhr) {
    if (xhr.statusText == "success") {
      window.location.href = '/videos/' + '22077152';
    }
}

请参阅:jQuery.ajax()

答案 1 :(得分:0)

你可以使用简单的方法:

$.post('http://your.ajax.url/', 'data=1&data2', function(response){
      console.log(response);
}, 'json');

答案 2 :(得分:0)

您将dataType设置为'json',因此jQuery.ajax需要一个json文本,但它会获得html,因此会抛出错误。只有在请求返回json时才将dataType设置为json。

答案 3 :(得分:0)

您将dataType设置为'json',因此jQuery.ajax需要一个JSON文本,但它会获取HTML,因此会引发错误。当您的请求返回JSON时,仅将dataType设置为json

当我在PHP中测试时,我们抛出没有json_encode的消息,并且没有错误。