简体   繁体   中英

Ajax request cross domain json data and display?

I have a function to get JSON data from another domain, but I don't know what is wrong with it. It never fires the success function and and no errors are returned.

$.ajax({
    url: "http://other-domain.com/sample/Json.aspx",
    dataType: "jsonp",
    type: 'get',
    crossDomain: true,
    jsonp: 'jsonp_callback',
    success: function() {
        alert('abc'); //when success take json data string but i din get in here
    }
});

Does anyone know what's wrong here?

Try:

$.get('http://other-domain.com/sample/Json.aspx', function(data) {
    alert(data);
});

You should get an alert with a json string (which is actually a string), than use jQuery.parseJSON(data) on it and you'll get a JSON object to work on.

If this doesn't work, maybe you need to check your get parameters.

Edit:

I just tried to set up this page right now:

<head>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

    <script>

        $(document).ready(function(){

            $.getJSON('http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?',function(data){

                alert(data.title);

            });

        });

    </script>

</head>

<body>

    ... WHATEVER ...        

</body>

and it works just fine, it's alerting the first title element in the data object.

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