1

我很难阅读以下 JSON(基本上没有错误,也没有任何反应),并希望能帮助我识别函数的问题。我不清楚 ajax 是否成功地抓住了它,或者它只是在之后没有正确读取它。任何帮助将非常感激!

这是 JSON 响应的结构:

   [{"attributes":{"type":"Account","url":"/services/data/v25.0/sobjects/Account/00130000015KJlWAAW"},"id":"00130000015KJlWAAW","locname":"Central Valley Ag","address":"607 North Robinson","city":"Hartington","state":"NE","postal":"68739","phone":"402-254-3354","web":"","lat":"42.627212","lng":"-97.269283"},{"attributes":{"type":"Account","url":"/services/data/v25.0/sobjects/Account/00130000015KJlXAAW"},"id":"00130000015KJlXAAW","locname":"Central Valley Ag","address":"709 Centennial Rd","city":"Wayne","state":"NE","postal":"68787","phone":"402-375-3510","web":"","lat":"42.235756","lng":"-96.998321"}]

这是处理 JSON 的脚本部分:

   function mapping(orig_lat, orig_lng, origin) {
                $(function () {
                    var dataType;
                    //jsonData is set to true
                    if (settings.jsonData == true) {
                        dataType = "jsonp";
                    } else {
                        dataType = "xml";
                    }
                    $.ajax({
                        type: "GET",
                        url: settings.dataLocation,
                        dataType: dataType,
                        crossDomain: true,                            
                        error: function(jqXHR, textStatus, errorThrown) {   
                    alert('Error Message: '+textStatus);
                    alert('HTTP Error: '+errorThrown);
                },
                        success: function (data) {
                            //After the store locations file has been read successfully
                            var i = 0;

                            if (settings.jsonData == true) {
                                var data = $.parseJSON(data);
                                //Process JSON
                                $.each(data, function () {
                                    var name = this.locname;
                                    var lat = this.lat;
                                    var lng = this.lng;
                                    var address = this.address;
                                    var address2 = this.address2;
                                    var city = this.city;
                                    var state = this.state;
                                    var postal = this.postal;
                                    var phone = this.phone;
                                    var web = this.web;
                                    web = web.replace("http://", "");
                                    var distance = GeoCodeCalc.CalcDistance(orig_lat, orig_lng, lat, lng, GeoCodeCalc.EarthRadiusInMiles);
                                    //Create the array
                                    locationset[i] = new Array(distance, name, lat, lng, address, address2, city, state, postal, phone, web);
                                    i++;
                                });
                            } else {
                                //Process XML
                                $(data).find('marker').each(function () {
                                    //Take the lat lng from the user, geocoded above
                                    var name = $(this).attr('name');
                                    var lat = $(this).attr('lat');
                                    var lng = $(this).attr('lng');
                                    var address = $(this).attr('address');
                                    var address2 = $(this).attr('address2');
                                    var city = $(this).attr('city');
                                    var state = $(this).attr('state');
                                    var postal = $(this).attr('postal');
                                    var phone = $(this).attr('phone');
                                    var web = $(this).attr('web');
                                    web = web.replace("http://", "");
                                    var distance = GeoCodeCalc.CalcDistance(orig_lat, orig_lng, lat, lng, GeoCodeCalc.EarthRadiusInMiles);
                                    //Create the array
                                    locationset[i] = new Array(distance, name, lat, lng, address, address2, city, state, postal, phone, web);
                                    i++;
                                });
                            }

更新:我添加了一个 ajax 错误处理程序作为状态,并在页面加载时得到以下 parseerror:

HTTP 错误:错误:未调用 jQuery172036075869924388826_1343923258757

然后,当我尝试运行该函数时,出现以下错误:

Uncaught TypeError: Property 'alert' of object [object Window] is not a function          dealer-locator-iframe2:273
$.fn.storeLocator.each.$.ajax.error dealer-locator-iframe2:273
f.Callbacks.o jquery.min.js:2
f.Callbacks.p.fireWith jquery.min.js:2
w jquery.min.js:4
f.ajaxTransport.send.d.onload.d.onreadystatechange
4

3 回答 3

1

It looks like you need to run $.parseJSON on the data variable. data is coming back as a string of JSON and you need to convert it into a JavaScript object literal.

Example:

var obj = $.parseJSON('{"name":"John"}');
alert( obj.name === "John" );

So place this immediately after if (settings.jsonData == true) in your success function:

var data = $.parseJSON(data);

The reason you're not getting any errors is because the $.each(function() {....}); isn't looping, because there isn't anything to loop on a string.

有一种非常简单的方法可以测试data变量的数据类型。success在函数的开头使用它:

alert(typeof(data));

如果你被string退回,那么你知道这就是问题所在。

于 2012-08-02T15:07:09.040 回答
0

您的 JSON 看起来像一个对象数组,您可以通过它周围的 [] 来判断。它是一个只有一个元素的数组,但如果你写这会出错

json.key

因为 json 不是一个对象,而是一个对象数组,所以 json.key 看起来你正在调用数组中不存在的一些 fncion“key”。您应该首先使用 json[0] 选择真实对象本身,例如

myJson = json[0]

然后

value = myJson.key1

如果您使用的是一些外部 API,这很可能是我之前遇到过的这个问题

于 2012-08-02T16:54:22.203 回答
0

$.each添加此行之前

data = eval("("+data+")");

您的其余代码将正常工作...

为什么你的代码不起作用?原因 - 因为您正试图在string. 首先将其string转换为JSON object. eval()转换stringjavascript对象。

于 2012-08-02T15:14:05.070 回答