1

为了解决无法在 IE 中运行的 ajax 请求的问题,我更改了代码。现在它在任何浏览器中都不起作用。奇怪的是,ajax 请求确实得到了正确的结果。只有这个结果没有被解析。如何解决这个问题?我需要更改一些标题吗?

所有函数都是 tbGeocoder 对象中的方法。

 $.ajax({
        url: 'http://nominatim.openstreetmap.org/search',
        type: 'GET',
        dataType: 'jsonp',
        jsonp: 'false',
        jsonpCallback: 'json_callback' + tbGeocoder.requestIndex,
        data: {
            format: 'json',
            q: input,
            limit: 1,
            json_calback: 'json_callback' + tbGeocoder.requestIndex
        },
        beforeSend: function(x) { 
            if (x && x.overrideMimeType) { 
                x.overrideMimeType("application/json;charset=UTF-8"); 
            } 
        },
        dataFilter: function (data, type) {
            console.log(data);
            console.log(type);
          /*for (key in data) {
                //console.log(key);
            }*/
            return data;
        },
        success: tbGeocoder.processRequestResult,

        error: function(data, textStatus, jqXHR) {
            console.log(textStatus);
        }
    });
    tbGeocoder.requestIndex++;
},
preProcessRequestResult: function (data, type) {
    console.log(data);
    console.log(type);
    for (key in data) {
        //console.log(key);
    }
    return data;
},
processRequestResult: function (data) {
    console.log('==>in loop for nominatim');
}

在 firebug 中,输入类似 input="Den Dolechh" 的控制台输出如下:

code address location
Den Dolech
undefined
jsonp
parsererror
Object { readyState=4, status=200, statusText="success"}

在网络状态中,您可以看到标题如下:

 Replyheaders
 Access-Control-Allow-Orig...   *
 Connection close
 Content-Length 445
 Content-Location   search.php
 Content-Type   application/json; charset=UTF-8
 Date   Thu, 05 Jul 2012 10:31:11 GMT
 Server Apache/2.2.14 (Ubuntu)
 TCN    choice
 Vary   negotiate
 X-Powered-By   PHP/5.3.2-1ubuntu4.17

 Requestheaders
 Accept */*
 Accept-Encoding    gzip, deflate
 Accept-Language    nl,en-us;q=0.7,en;q=0.3
 Connection keep-alive
 Host   nominatim.openstreetmap.org
 Referer    http://localhost/locations/add
 User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1

回复是:

 [{"place_id":"44757488","licence":"Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.","osm_type":"way","osm_id":"36859933","boundingbox":["51.4484252929688","51.449333190918","5.48478031158447","5.48527336120605"],"lat":"51.4489435225293","lon":"5.48514453068994","display_name":"Den Dolech, Eindhoven, Samenwerkingsverband Regio Eindhoven, Noord-Brabant, 5600 MB, Nederland","class":"highway","type":"residential"}]

我该如何解决?我得到了数据,但我怎么把它拿出来?

4

3 回答 3

1

你不应该直接使用数据processRequestResult

使用data.d并将其存储在某个变量中。

var tempData = data.d;

现在使用tempData而不是data.

于 2012-07-05T14:02:11.093 回答
0

您附加的 JSON 是有效的 JSON,而不是 JSONP

// Some valid JSON responses:
// (even yours is valid JSON)
{x: 1}
[{x: 1}, {x: 2}]

// Some valid JSONP responses:
foo({x: 1})
bar([{x: 1}, {x: 2}])
于 2012-07-05T13:33:33.473 回答
0

好的,这很烦人。答案是对错字的修复!

线

 json_calback: 'json_callback' + tbGeocoder.requestIndex

应该

data: {
            format: 'json',
            q: input,
            addressdetails: 1,
            limit: 1,
            json_callback: 'json_callback' + tbGeocoder.requestIndex
        },

注意回调中的双'l'。我还添加了地址详细信息,因为这就是我一直在寻找的 ;-)

有了这个,我得到了正确的 jsonp 共鸣:

 json_callback11([{"place_id":"44757488","licence":"Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.","osm_type":"way","osm_id":"36859933","boundingbox":["51.4484252929688","51.449333190918","5.48478031158447","5.48527336120605"],"lat":"51.4489435225293","lon":"5.48514453068994","display_name":"Den Dolech, Eindhoven, Samenwerkingsverband Regio Eindhoven, Noord-Brabant, 5600 MB, Nederland","class":"highway","type":"residential","address":{"road":"Den Dolech","residential":"Eindhoven","suburb":"Eindhoven","city":"Eindhoven","county":"Samenwerkingsverband Regio Eindhoven","state":"Noord-Brabant","postcode":"5600 MB","country":"Nederland","country_code":"nl"}}])

这是正确处理的。这是一个需要技巧(使 json_callback 和 jsonp_callback 相同......)来解决相同的起源问题和浏览器之间的差异。这适用于 Windows 上的 IE9、FF 和 Chrome,以及 Mac 上的 FF、Chrome 和 Safiri。

于 2012-07-06T13:15:20.413 回答