0

I have the following code and i am trying to retrieve the userid , firstname and some values from JSON but I am getting undefined.

How can i fix this?

 function getVal(val1,val2) {       
    var myval2 =URL +"webUser?email="+email;
    var uId='';


    $.ajax({
        url: myval2,
        dataType: "json",

         cache: false, 
        success: function(data, textStatus, jqXHR) {



            jQuery.each(data, function(k,v) {

             console.log( " k "  +k  + " v" + v);

             uId =v.uId;



             });       
    },
        error: function(jqXHR, textStatus, errorThrown) {
            console.log('FAILED to get  JSON from AJAX call' + jqXHR + textStatus + errorThrown);

        }
    });       
      console.log("userid " +userId);
     return userId;

   }    

JSON

{
  "userId": 2,
  "emailAddress": "some@some.com",
  "roleId": 1,
  "firstName": "Hulk",
  "lastName": "Ogan",
  "company": "MTT",
  "title": "Lord Vader",
  "phoneWork": "",
  "phoneMobile": "",
  "lastModifiedBy": "system",
  "versionNumber": 1
}
user244394
  • 13,168
  • 24
  • 81
  • 138
  • 1
    [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) / [Why is my variable unaltered after I modify it inside of a function?](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Jonathan Lonowski Dec 14 '16 at 05:41
  • are you sure you need to iterate through all the data ? or you need just one data (userID in this case) ? – Shashi Dec 14 '16 at 05:43

4 Answers4

2

You can simply get value of userid and other properties like this

userID = data.userId;

No need of loop because your data is only a single object you will need loop if your data was an array of objects

Mairaj Ahmad
  • 14,434
  • 2
  • 26
  • 40
0

You can simply get value of userid and other properties like this

userID = data['userId'];

var data = {
  "userId": 2,
  "emailAddress": "some@some.com",
  "roleId": 1,
  "firstName": "Hulk",
  "lastName": "Ogan",
  "company": "MTT",
  "title": "Lord Vader",
  "phoneWork": "",
  "phoneMobile": "",
  "lastModifiedBy": "system",
  "versionNumber": 1
};
console.log(data['userId']);
Note: you can't return any values from a asynchronous ajax request, do all your logic in the success function of your ajax call
madalinivascu
  • 32,064
  • 4
  • 39
  • 55
0

One problem I can see is that your json record is just that: a single record; so, you don't need $.each to iterate through the ajax result - that would only be necessary if your returned data consisted of a json array with potentially many records.

Other than that, you will just need to do some bug tracking:

  1. Is your http action on your website set up to handle GET actions? If it needs a POST action you will need to specify that in your ajax call.
  2. Leading on from 1 - are you certain that your call to the url does return a json result? Can you navigate to that url in a browser and see the json result?
  3. If both of those are taken care of, check what "data" contains when it returns. If it is empty or not well formed json, figure out why.
codemonkey65
  • 120
  • 1
  • 6
0

You don't need to jQuery.each, because you don't have a JsonArray and you have JsonObject. You can easily get you data with:

userID = data.userId;

and

userID = data['userId'];

(like as Madalin & Leopars answers)

madalinivascu
  • 32,064
  • 4
  • 39
  • 55
Alireza MH
  • 573
  • 8
  • 25