Solved

JQuery - $.ajax returns UNDEFINED

Posted on 2014-01-21
6
892 Views
Last Modified: 2014-01-26
I'm calling the following:  The service works and returns  JSON - tested in browser and fiddler
But when I try and parse the response I get UNDEFINED.

Here's the code

 $.ajax({
                            type: "GET",
                            url: url,
                            data: params
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: OnSuccess,
                            error: OnError
                        });



  function OnSuccess(data, status) {

           alert(status);
           alert(data);    // UNDEFINED

 var json = JSON.parse(data)   // UNEXPECT TOKEN 0




ANY IDEAS?
THX
0
Comment
Question by:JElster
  • 4
  • 2
6 Comments
 
LVL 1

Expert Comment

by:fostersoft
ID: 39798966
Do you have check what the services is suppose to return with the same params ?

Can you enter in debug mode to see what you function really return to your ajax query ?
0
 
LVL 1

Author Comment

by:JElster
ID: 39798977
In debugger ... look correct. but how do I get the values and why the errors?

data:  Object
  Get_UserResult: Object
   FIRSTNAME: "John"
   LASTNAME: "Smtih
0
 
LVL 1

Assisted Solution

by:fostersoft
fostersoft earned 500 total points
ID: 39798987
Your services is in wich programming language ?

First i think you don't need to do " JSON.parse(data) " because the result is suppose to be an object. You be suppose to call directly your property. Ex:
ALERT(data.FIRSTNAME)

Open in new window

If your in asp.net(not mvc page but in an aspx page) you can try
data.m.FIRSTNAME 

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 1

Author Comment

by:JElster
ID: 39798997
WCF Service JSON

data.FIRSTNAME is Undefined
??????
0
 
LVL 1

Accepted Solution

by:
JElster earned 0 total points
ID: 39799047
WebMethod  had
BodyStyle = WebMessageBodyStyle.Wrapped,
Which puts a the service name around the JSON.

I remove it and it works.

data:  Object
  Get_UserResult: Object
   FIRSTNAME: "John"
   LASTNAME: "Smtih
0
 
LVL 1

Author Closing Comment

by:JElster
ID: 39809863
worked
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question