简体   繁体   中英

jQuery JSON AJAX Request to different domain

I'm trying to make an AJAX request to https://api.mojang.com/users/profiles/minecraft/USERNAME , this should return JSON data (not sure if JSONP)

When I click on the button which will perform the AJAX request I get this:

http://prntscr.com/8xswr1 (Google Chrome Console)

When I double click on the 'dude1?callback...' I get this:

http://prntscr.com/8xsx7q

which contains the JSON data I want returned, meaning I am making the request and getting the JSON data but I can't parse it or get the information

Code:

<!DOCTYPE html>
<html>
    <head>
        <title>Website</title>
        <script src="https://code.jquery.com/jquery-2.1.4.js"></script>
        <script>
            function getInfo() {
                username = $('#username').val();
                $.ajax({
                    type: 'GET',
                    url: "https://api.mojang.com/users/profiles/minecraft/" + username,
                    //crossDomain: true,
                    dataType: 'jsonp',
                    success: function (data) {
                        alert(data.name);
                    }
                });
            }
        </script>
    </head>
    <body>
        <form>
            Username: <input type="text" id="username" name="username"/>
        </form>
        <button onclick="getInfo();">Submit</button>
        <span id="info">

        </span>
    </body>
</html>

Also, if I change the 'dataType' parameter to 'json' I get this error http://prntscr.com/8xsy9e

this should return JSON data (not sure if JSONP)

It's JSON.

When I click on the button which will perform the AJAX request I get this

That's a typical error for trying to execute JSON as if it were JSONP.

When I double click on the 'dude1?callback...' I get this

Yup, JSON. Not JSONP.

Also, if I change the 'dataType' parameter to 'json' I get this error

Yup, the site you are requesting data from isn't giving your site permission to read it using your visitor's browsers.


Cross-origin Ajax requires that the site you are getting the data from co-operates with you to provide it.

Fetch the data with your server instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM