jQuery Ajax方法没有返回XHR对象

Nat*_*win 4 ajax jquery xmlhttprequest

更新1:我还没弄清楚发生了什么,但这似乎与我的项目有关.在创建一个简单的测试页面后,我能够验证getJSON实际上是否确实返回了一个XHR对象.

更新2:哇,这很奇怪.在做了更多测试之后,我发现如果我指定"callback =?" URL字符串中的参数XHR对象未正确返回.但是,如果我没有指定"callback =?" 参数,正确返回XHR对象.问题是,我正在调用JSONP服务,所以"回调=?" 参数是必需的.

关于为什么会出现这种情况的任何想法?

更新3:这里有一些独立的代码示例来说明问题.在第一个示例中,未定义console.log(请求).当我在第二个代码示例中对回调参数进行硬编码时,console.log(request)是XHR对象.

<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    </head>
    <body>
        <script>
            $(document).ready(function() {
                var request = $.getJSON('http://localhost?callback=?', function(data) {

                });
                console.log(request);
            });
        </script>
    </body>
</html>

<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    </head>
    <body>
        <script>
            $(document).ready(function() {
                var request = $.getJSON('http://localhost?callback=callback', function(data) {

                });
                console.log(request);
            });
        </script>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

更新4:根据下面的regilero评论,我切换到使用$ .ajax方法并通过数据对象传递参数.以下是请求的完整代码:

var request = $.ajax({
    data: {
        f: 'json',
        geometry: '{x:44.203642291681845,y:-95.79085806500001}',
        geometryType: 'esriGeometryPoint',
        imageDisplay: '727,500,96',
        layers: 'all',
        mapExtent: '-179.462733065,16.116769346042226,-51.669764315000016,71.57609342040729',
        returnGeometry: false,
        tolerance: 10
    },
    dataType: 'jsonp',
    success: function(data) {
        console.log(data);
    },
    url: 'http://server.arcgisonline.com/ArcGIS/rest/services/Specialty/Soil_Survey_Map/MapServer/identify'
});
console.log(request);
Run Code Online (Sandbox Code Playgroud)

如果我在config对象中指定"dataType:'jsonp'",则console.log(request)再次未定义.但是,如果我指定"dataType:'json'",则console.log(request)是XHR对象.

此行为与我在$ .getJSON快捷方式中遇到的情况一致.


原始问题

根据stackoverflow问题/答案:在此站点上使用jQuery和许多其他问题/答案中止Ajax请求,jQuery Ajax方法应该返回XHR对象.

但是,当我运行以下代码时,请求是"未定义".

var request = $.getJSON(url, function(data) {
    console.log(data);
});

console.log(request);
Run Code Online (Sandbox Code Playgroud)

我错过了jQuery的变化吗?我正在使用1.4.4.

lon*_*day 5

jsonp不使用XMLHTTPRequest.它是同源策略的解决方法,适用于所有XMLHTTPRequests; 它通过将<script>标记插入DOM来工作,要求服务器返回包含在由callback=?参数表示的JS函数中的JSON对象.

由于未使用XMLHTTPRequest,因此不会返回该类型的对象.