我很难阅读以下 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