1

I have an ASP.NET MVC Web API that I am calling from an $.ajax() method. The correct JSON is returned from my API, but the object is not accessible. The error received in my console when trying to log the value for "Name" is:

Uncaught TypeError: Cannot read property 'Name' of undefined

JSON:

[{"id":2,"Name":"thom","Picture":"thom.jpg","About":"I'm a guy. This is my profile. Now quit staring and get out of here.","Location":"London"}]

jQuery:

$.ajax({
            cache:false,
            type: 'GET',
            dataType: 'json',
            url: 'http://localhost:3235/Users/searchUsers?callback=?&searchString=' + searchString,
            complete: function (data) {
                console.log(data[0].Name);
            }
        });

Any help would be appreciated. Thanks!

7
  • Try logging what data is. Commented May 2, 2013 at 15:40
  • Can you show what logs if you simply console.log(data) to see what is returning, rather than just knowing Name is not there Commented May 2, 2013 at 15:40
  • You are probably returning the result with a content-type of text/html. Either JSON.stringify(result) in JS or send the correct headers with the response. Commented May 2, 2013 at 15:40
  • Even if header is wrong, it would not matter if he is specifying that it is json. Commented May 2, 2013 at 15:41
  • jQuery automatically parses it for you when you include dataType:"json" as is being done in the code above. Commented May 2, 2013 at 15:41

3 Answers 3

9

I think you mean to use the success function. The complete function doesn't take data as a parameter.

Sign up to request clarification or add additional context in comments.

4 Comments

I can't believe I didn't notice that too :P
+1. I can't believe as well. Was thinking that squint solved it :D
@Ejay: Yeah, I totally missed the boat on that one. Didn't even consider the callback type. Though I wonder if the issue in Yatrix's answer will come into play too.
Success hadn't been working, but it's magically working again. Good to know that 'complete' doesn't take data. Thanks everyone!
4

From the docs:

complete
Type: Function( jqXHR jqXHR, String 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 ("success", "notmodified", "error", "timeout", "abort", or "parsererror").

The method's first parameter is not the received data. You can get it though the jqXHR object but I don't think you really need to use this option. Use success instead:

success
Type: Function( PlainObject data, String textStatus, jqXHR 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.

Comments

1

ya complete fires after the service call is being made and it does not contain data from the service response..

use

$.ajax({
url:'ur url',
type:'GET'
success:function(data){
// way to acces ur object code goes here


console.log(data[0].Name);


},
error:function(){
// Error handling
}
});

happy coding

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.