Solved

JQuery - $.ajax returns UNDEFINED

Posted on 2014-01-21
6
858 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 1

Author Comment

by:JElster
Comment Utility
WCF Service JSON

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

Accepted Solution

by:
JElster earned 0 total points
Comment Utility
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
Comment Utility
worked
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
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)

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now