简体   繁体   中英

How to use JSON returned by jQuery AJAX call?

I have the following ajax call in my javascript. This call executes correctly, the appropriate action is taken on the server, and the server returns status 200 and a JSON object containing: a string name result containing the value "OK" and an array of tuples called aaData . I can see this returned data by spying out the results in Chrome.

My problem is that while alert("Hello") executes as expected, and alert(response) displays "object" (as I would expect); alert(response["result"] displays "undefined". It appears to me that response has already been parsed into a javascript object and I should be able to use it. Am I wrong? Is there something else I need to do to the object? Or am I addressing its elements incorrectly.

$.ajax( {type:        "PUT",
     url:         "/fund/${fund.name}/contacts",
     data:        payload,
     contentType: "application/json",
     processData: false,
     cache:       false,
     dataType:    "application/json",
     complete:    function(response) {
            alert("Hello");
            alert(response);
            alert(response["result"]);
                if (response["result"] != "OK")
            {
                alert(response["result"])
            }
            else
            {
                $("#fund-contacts").dataTable( {"aaData": response["aaData"]} )
            }
            $("#fund-contact-entry").dialog( "close" );
            alert("Hello");
              }
    }
);

Use the success callback instead of complete .

From the manual

success(data, textStatus, jqXHR)

A function to be called if the request succeeds. The function gets passed three arguments: The data returned from the server, formatted according to the dataType parameter; a string describing the status; and the jqXHR (in jQuery 1.4.x, XMLHttpRequest) object

complete(jqXHR, textStatus)

A function to be called when the request finishes (after success and error callbacks are executed). The function gets passed two arguments: The jqXHR (in jQuery 1.4.x, XMLHTTPRequest) object and a string categorizing the status of the request

Edit: Also, what @hobbs says above

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM