我正在尝试创建一个应用程序,根据他使用Google Maps API的坐标显示用户地址,代码在Eclipse IDE中正常运行。
但是当我在IIS上托管它时,我收到以下错误。
XMLHttpRequest无法加载 https://maps.googleapis.com/maps/api/geocode/json?latlng=30.0043704,31.176554&sensor=true&key=AIzaSyCEZN8UB34nbx3D-17xk1J-DRnNCF_kjm4。 请求标头字段不允许使用Content-Type 预检响应中的Access-Control-Allow-Headers。
我的代码在
之下var position = null;
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(aPosition) {
position = aPosition;
jQuery.sap.require("sap.m.MessageToast");
$.sap.log.error("LAT:" + position.coords.latitude + " LON:" + position.coords.longitude);
var oGeoModel = new sap.ui.model.json.JSONModel();
var aData = jQuery.ajax({
type: "GET",
contentType: "application/json",
url: "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + position.coords.latitude + "," + position.coords.longitude +
"&sensor=true&key=AIzaSyCEZN8UB34nbx3D-17xk1J-DRnNCF_kjm4",
dataType: "json",
async: false,
crossDomain: true,
success: function(data, textStatus, jqXHR) {
oGeoModel.setData({
modelData: data
});
//alert("success to post");
myView.byId("txtVendCountry").setValue(oGeoModel.getProperty("/modelData/results/4/formatted_address"));
myView.byId("txtVendRegion").setValue(oGeoModel.getProperty("/modelData/results/2/formatted_address"));
myView.byId("txtVendorAddress").setValue(oGeoModel.getProperty("/modelData/results/0/formatted_address"));
console.log(oGeoModel);
}
});
console.log(oGeoModel);
});
} else {
sap.m.MessageToast.show("Sorry, your browser does not support geolocation services.");
}
任何帮助将不胜感激 提前致谢
答案 0 :(得分:0)
错误日志显示问题与Content-Type有关。尝试以下方法。这很简单。
var url = "https://maps.googleapis.com/maps/api/geocode/json?latLang=" --your lat lang" + "&sensor=false&----yourAPIKey4---&callback=getJSON";
$.getJSON(url, function(data) {
//set data to model here
});