$ .ajax():不允许原点为null

Ran*_*lue 5 ajax jquery

我刚刚开始$.ajax().这是我的代码:

<html>
<head>
    <title>Commons app</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
</head>
<body>
<script type='text/javascript'>
    if($) console.log('jQuery loaded!\n');

    $(function () {
        $.ajax({
            url: 'http://en.wikipedia.org/w/api.php?action=query&list=allimages&ailimit=5&aifrom=Albert&aiprop=dimensions|mime&format=jsonfm&callback=?'
        })
        .done(function () { console.log('Yay!'); })
        .fail(function () { console.log('Error!'); })
        .always(function () { console.log('Complete!'); });
});
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

出于某种原因,我收到以下错误消息:

XMLHttpRequest无法加载 http://commons.wikimedia.org/w/api.php?action=query&generator=allimages&gailimit=4&gaifrom=T&prop=imageinfo&iiprop=url&callback= ?, Access-Control-Allow-Origin不允许使用null.

Yur*_*kiy 1

更新:此方法从 jQuery 1.9 开始已弃用。不再起作用了。

旧答案:

试试这个(已更新):

$.support.cors = true;

$(function () {
     $.ajax("http://commons.wikimedia.org/w/api.php?action=query&generator=allimages&gailimit=4&gaifrom=T&prop=imageinfo&iiprop=url&callback=?")
     .complete(function () { alert('complete'); })
     .error(function () { alert('error'); })
     .success(function (data) { alert(data); });
});
Run Code Online (Sandbox Code Playgroud)

有关 jQuery.support.cors 设置的更多信息,请参见此处:jQuery.support

PS谢谢你提出了一个很好的问题!我相信它对我将来也很有用。