1

I'm playing around with the http://randomuser.me/documentation.html api. It just generates random user data in json format. However, I can not seem to parse the response. Whenever I do, I just get undefined objects.

Here is my code:

<script>
  $.ajax({
        url: 'http://api.randomuser.me/',
        dataType: 'json',
        success: function (data) {
            console.log(data);
            alert(data.user);
        }
    });
</script>

<div id="user"></div>
2
  • 2
    jsfiddle.net/s6oa20e2 Example: data.results[0].user.name.first Commented Aug 8, 2014 at 20:59
  • 1
    That domain has the correct CORS header set to allow cross-domain AJAX so it shouldn't be a cross-domain issue. Commented Aug 8, 2014 at 21:00

4 Answers 4

2

Take a good look at the JSON being returned. You are getting the properly-decoded data, but the data structure does not match what you are expecting.

The user field is inside of an array called results which you have to access with an index. For example, to get the first user:

var user = data.results[0].user;

You can also use a loop structure if you are requesting multiple results.

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

Comments

0

You can't make AJAX requests to domains that are different from your own (that is, the one where the script is being executed). This happens due to security reasons.

If you want to do something like that, create a PHP script (or something else) that retrieves the data from api.randomuser.me, and then use AJAX to request the PHP script (that you hosted on the same place as your JS script).

8 Comments

But I am getting a responce with data.
@Mark fgiobergia based his reply on the same origin security policy which randomuser.me probably circumvents via cross-origin resource sharing (en.wikipedia.org/wiki/Cross-origin_resource_sharing) because of the nature of their service.
@Mark, I have to run for now, but I'll come back to your question later tonight and help you out if you haven't gotten a good answer by then.
Yes. See my comment on your question for an example of how to parse it.
Okay, I was wrong. Anyway, your problem is with the way you access the result. data.user isn't among the data returned by the API. On the other hand, if you try to access, say, data.results[0].user.gender, you'll get the result you want ;) EDIT: whoops, didn't realize that @JasonP figured that out before me
|
0

Yes, it conflicts with the same domain policy. You have to use js script file to do it. Or use "jsonp" to do it. Since the json data is from 3rd party web server, the "jsonp" is not working for you in this case.

Comments

0

Whenever working with unfamiliar JSON, I suggest formatting it nicely at http://jsonlint.com

In this case, the API call returns a JSON object with a results array that contains the user object.

data.results[0].user;

will give you the user object you are looking for, which you can use in code.

In order to see the contents of an object in an alert dialog, you'll first need to pass the object into JSON.stringify().

<script>
  $.ajax({
        url: 'http://api.randomuser.me/',
        dataType: 'json',
        success: function (data) {
            console.log(dataObj);
            alert(JSON.stringify(dataObj.results[0].user));
        }
    });
</script>

3 Comments

The data should already be decoded as JSON if dataType: 'json'
data is already a javascript object, parsing it would cause an error.
@nullability You are correct, that'll simplify my answer

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.