Solved

JQuery - $.ajax returns UNDEFINED

Posted on 2014-01-21
6
978 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Stressed Out?

Watch some penguins on the livecam!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

707 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