?yb? 2014-09-04 13:55 采纳率: 0%
浏览 43

Javascript AJAX返回错误

I have an AJAX call:

            $('#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;
            });

It should be successful, but I get the alert("Error: "+data) alert. The data is [object Object]. So the alert just says Error: [object Object]

In my console.log(data)

    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

As you can see it does show the responseText: "drew" which is what I want. I'm just curious to know why is passing through my fail function, and not my success. Please let me know if there is anything else you would need to help me solve this.

  • 写回答

3条回答 默认 最新

  • weixin_33728708 2014-09-04 14:07
    关注

    According to the jquery documentation, the error parameter takes three inputs as parameters:

    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."

    The errormessage, and the error can be viewed if you modify the error function to take three parameters input and access the second argument to know what the error string is.

    If you do this, it becomes easy to spot the error and know why the request failed. Once you spot the error, it becomes easy to fix it.

    unexpected token probably means you've got a corrupted JSON response from the server. Also make sure the response the server component sends is of type JSON.

    Set the response type in the server, for example, if the response is json:

    response.setContentType("application/json");
    

    See Also:

    What is the correct JSON content type?

    Refer: http://api.jquery.com/jquery.ajax/

    评论

报告相同问题?