【发布时间】:2018-04-27 02:55:53
【问题描述】:
<script type="text/javascript">
navigator.geolocation.getCurrentPosition(success, error);
function success(position) {
var GEOCODING = 'https://maps.googleapis.com/maps/api/geocode/json?latlng=' + position.coords.latitude + '%2C' + position.coords.longitude + '&language=en';
$.getJSON(GEOCODING).done(function(location) {
$('#country').html(location.results[0].address_components[5].long_name);
$('#state').html(location.results[0].address_components[4].long_name);
$('#city').html(location.results[0].address_components[2].long_name);
$('#address').html(location.results[0].formatted_address);
$('#latitude').html(position.coords.latitude);
$('#longitude').html(position.coords.longitude);
})
$.ajax({
url: 'post/filter/',
data: {
lat: position.coords.latitude,
lon: position.coords.longitude,
},
type: "POST",
dataType: 'json',
});
}
function error(err) {
console.log(err)
}
这是我的代码。我需要将纬度和经度作为 ajax 请求传递给服务器端以获取值。通过使用此代码,我能够做到这一点。
我的vue js代码是
<div id="categorie">
<div>{{vector.name}}</div>
</div>
<script>
categorie = new Vue({
el: '#categorie',
data: {
vector: {},
},
methods: {
search_category: function success(position) {
navigator.geolocation.getCurrentPosition(success, error);
var GEOCODING = 'https://maps.googleapis.com/maps/api/geocode/json?latlng=' + position.coords.latitude + '%2C' + position.coords.longitude + '&language=en';
$.getJSON(GEOCODING).done(function(location) {
$('#country').html(location.results[0].address_components[5].long_name);
$('#state').html(location.results[0].address_components[4].long_name);
$('#city').html(location.results[0].address_components[2].long_name);
$('#address').html(location.results[0].formatted_address);
$('#latitude').html(position.coords.latitude);
$('#longitude').html(position.coords.longitude);
})
var filter = {};
var self=this;
filter['category'] = position.coords.latitude;
filter['subcategory'] = position.coords.longitude;
$.ajax({
"url": "post/filter",
data: filter,
dataType: "JSON",
type: "POST",
success: function(e) {
self.vector = e.data;
console.log(e);
},
});
}
}
})
</script>
但是当我使用 vue js 代码时,我无法发送 ajax 请求?任何人都可以解决我的问题有人可以帮我解决这个问题吗?
【问题讨论】:
-
你能澄清一下“我无法发送 ajax 请求”是什么意思吗?你有错误吗?错误是什么?
标签: javascript jquery vue.js vue-component vue-router