2014-09-04 90 views
1

我有一个AJAX调用:的Javascript AJAX返回错误

  $('#testyo').click(function(){ 
      $.ajax({ 
       type: 'GET', 
       url: "../messages/drew", 
       dataType: 'JSON', 
       success:function(data){ 
       alert(data); 
       }, 
       error: function(data) 
       { 
        console.log(data); 
        alert("Error: "+data); 
       } 
      }); 
      return false; 
      }); 

它应该是成功的,但我得到的警报( “错误:” +数据)警报。数据是[对象对象]。因此警报只是说错误:[对象的对象]

在我的console.log(数据)

Object {readyState: 4, 
getResponseHeader: function, 
getAllResponseHeaders: function, 
setRequestHeader: function, 
overrideMimeType: function…} 
abort: function (statusText) {always: function() {complete: function() {done: function() {error: function() {fail: function() {getAllResponseHeaders: function() {getResponseHeader: function (key) {overrideMimeType: function (type) {pipe: function (/* fnDone, fnFail, fnProgress */) {progress: function() {promise: function (obj) {readyState: 4responseText: "drew"setRequestHeader: function (name, value) {arguments: nullcaller: nulllength: 2name: ""prototype: Object__proto__: function Empty() {}<function scope>state: function() {status: 200statusCode: function (map) {statusText: "OK"success: function() {arguments: nullcaller: nulllength: 0name: ""prototype: Object__proto__: function Empty() {}<function scope>then: function (/* fnDone, fnFail, fnProgress */) {__proto__: Object 

正如你可以看到它确实表明了responseText的:“画”,这是我想要的。我只是想知道为什么要通过我的失败功能,而不是我的成功。请让我知道是否还有其他需要帮助我解决的问题。

+3

使用'console.log'在javascript控制台中打印错误并检查您的对象。 – Alvaro 2014-09-04 13:57:09

+0

这不是我所做的吗? – user3591126 2014-09-04 13:59:41

+0

收到的HTTP代码是什么? – Fractaliste 2014-09-04 14:01:27

回答

1

根据jQuery的文档,所述误差参数有三个输入作为参数:

error:

Type: Function(jqXHR jqXHR, String textStatus, String errorThrown) 

A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error."

的errormessage的,并且可以如果修改的误差函数取三要观看的错误参数输入并访问第二个参数以知道错误字符串是什么。

如果你这样做,很容易发现错误并知道请求失败的原因。一旦发现错误,修复它就变得很容易。

unexpected token可能意味着你已经从服务器得到了损坏的JSON响应。还要确保服务器组件发送的响应是JSON类型。

设置服务器响应类型,例如,如果响应是JSON:

response.setContentType("application/json"); 

另请参见:

What is the correct JSON content type?

参见:http://api.jquery.com/jquery.ajax/

+0

谢谢你。我的错误现在说“SyntaxError:意外的令牌d” – user3591126 2014-09-04 14:12:33

+0

是的,我JSON被损坏。修正了它,并修复了它。谢谢! – user3591126 2014-09-04 14:25:41

+0

@ user3591126 - 欢迎光临! – BatScream 2014-09-04 14:30:00

0

我看到你得到200响应,但可能你的服务器返回错误的内容类型fo r json或者完全无效的json。

在firebug或chrome的Firefox中,尝试检查响应和响应头像内容类型。

https://stackoverflow.com/a/11112320/1641941

0

当你responseText回报 “吸引”,你的反应是不是JSON,但字符串。删除dataType: 'JSON'和jQuery将使用自动检测。然后success函数将被调用。

JSON应该是类似{"myText": "drew"}