【问题标题】:jquery not parsing ajax resultjquery不解析ajax结果
【发布时间】:2012-07-05 10:44:43
【问题描述】:

为了解决 ajax 请求在 IE 中不起作用的问题,我更改了代码。现在它在任何浏览器中都不起作用。奇怪的是,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"}]

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

【问题讨论】:

  • 在 'success' 上尝试将数据传入:tbGeocoder.processRequestResult,输入数据,以便它能够在此函数中对其进行处理。
  • 问题是永远无法达到成功函数,因为 ajax 判断存在解析错误。

标签: javascript ajax jquery http-headers


【解决方案1】:

你不应该直接在processRequestResult中使用数据

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

var tempData = data.d;

现在使用tempData 代替data

【讨论】:

  • 我不明白这一点。由于解析错误,甚至没有达到成功功能。即便如此,data 是“未定义的”,那么 data.d 怎么会有任何内容呢?
【解决方案2】:

您附加的 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}])

【讨论】:

  • 这似乎是问题,但是如何解决呢?我需要 jsonp 请求以确保我也在 IE 中得到响应(由于相同的来源问题,否则我不会得到响应)。我需要欺骗 ajax 来接受传入的 json 对象,现在它似乎只接受 jsonp。
  • 您必须修改您的服务器代码或使用 JSON 并手动调用回调。
【解决方案3】:

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

线

 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。

【讨论】:

    猜你喜欢
    • 2019-05-28
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    • 1970-01-01
    • 2012-04-30
    • 2012-05-02
    • 1970-01-01
    • 2013-11-25
    相关资源
    最近更新 更多