我遇到了从我的JSONP字符串中获取值的问题,我通过AJAX请求获取了这些值,如下所示:
result({"respond":1,"paging":{"stillmore":0,"perpage":10,"callpage":1,"next":2,"previous":0,"pages":1,"result":"4"},"message":"","result":[{"ID":"1","user_email":"admin@domain.com","custom_fields":{"user_longitue":"51.5081","user_latitude":"0.0878"}},{"ID":"2","user_email":"admin@domain.com","custom_fields":{"user_longitue":"51.9081","user_latitude":"0.2878"}},{"ID":"3","user_email":"admin@domain.com","custom_fields":{"user_longitue":"51.6081","user_latitude":"0.1878"}}]})
我的javascript应该采用 user_latitude 和 user_longitue 的值,但由于某些原因无法实现。
var myData = JSON.parse(jsonString);
$(document).ready(function () {
var distanceObj = [],
i = 0;
$.each(myData.result, function (a, b) {
distanceObj[i] = { distance: hesapla(51.41140000, -0.22600000, b.custom_fields.user_longitude, b.custom_fields.user_latitude), location: a };
++i;
});
distanceObj.sort(function(a,b) {
return parseInt(a.distance) - parseInt(b.distance)
});
$.each(distanceObj, function(a, b) {
$('#groups').append('<li>' + b.location + ': ' + b.distance + 'm</li>');
});
console.log(distanceObj);
function hesapla(meineLongitude, meineLatitude, long1, lat1) {
erdRadius = 6371;
meineLongitude = meineLongitude * (Math.PI / 180);
meineLatitude = meineLatitude * (Math.PI / 180);
long1 = long1 * (Math.PI / 180);
lat1 = lat1 * (Math.PI / 180);
x0 = meineLongitude * erdRadius * Math.cos(meineLatitude);
y0 = meineLatitude * erdRadius;
x1 = long1 * erdRadius * Math.cos(lat1);
y1 = lat1 * erdRadius;
dx = x0 - x1;
dy = y0 - y1;
d = Math.sqrt((dx * dx) + (dy * dy));
return Math.round(d * 1000);
};
});
请在这里帮我解决我的JSFIDDLE:
编辑:这是AJAX调用的代码:
$.ajax({
url: 'mydomain.com/api',
async: false,
jsonpCallback: 'callback',
contentType: 'application/json; charset=utf-8',
dataType: 'jsonp',
timeout: 2000,
success: function (data, response) {
if (response === 'success') {
if (data !== undefined && data.result !== undefined) {
$.each(data.result, function (i, item) {
})
}
}
}
});
答案 0 :(得分:2)
您的AJAX通话存在一些问题。清理它应该可以让你正确地获取数据。
$.ajax({
url: 'mydomain.com/api',
// You should never use `async: false`
// P.S. JSONP adds a `<script>` tag, so this does nothing
//async: false,
// This will change the request to `mydomain.com/api?callback=result`
// instead of `mydomain.com/api?callback=<someRandomFunctionName>`
// You can get rid of this if you set your server to
// use `$_GET['callback']` when it creates its response
jsonpCallback: 'result',
// `contentType` is the `Content-type` of the REQUEST body,
// it does nothing here
//contentType: 'application/json; charset=utf-8',
// jsonp is a "hack" to get around the same-origin policy
// it adds a `<script>` tag to the DOM to get the data
// the response should be a function call to the "callback"
// with the data you want to return
dataType: 'jsonp',
timeout: 2000,
success: function (data) {
// This is only called on success,
// so if we're here, we can assume we have the data
// This is all asynchronous, so you need to put all
// code that needs access to this inside this callback
// or use `$.ajax(...).done(function(data){...});`
$.each(data.result, function(i, v){
console.log(v.ID);
});
}
});