我正在尝试实现一个函数,该函数将从Google Places API中获取一些详细信息,以便以后可以用来制作InfoWindow
。
这是我的代码:
this.getData = function() {
var rating;
var reviews = [];
var address;
var phone_no;
//var dhabaImg;
var placeURL = `https://maps.googleapis.com/maps/api/place/details/json?placeid=${self.placeID}&key=${mapApiKey}`;
$.ajax({
url: placeURL,
dataType: 'jsonp',
type: "GET",
crossOrigin: true,
xhrFields: { withCredentials: true },
cache: false
}).done(function(){
rating = data.result.rating.toString();
address = data.result.formatted_address.toString();
phone_no = data.result.formatted_phone_number.toString();
$.each(data.result.reviews, function(i, review) {
reviews.push(`<li>${review.text}</li>`)
});
self.content = `<h2>${self.title}</h2>
<h3>${rating}</h3>
<h3>Phone: ${phone_no}</h3>
<p><b>Address:</b> ${address}</p>`
+ '<ol class="ratings">' +reviews.join(' ')+ '</ol>';
}).fail(function(){
self.content = `<h2>${self.title}</h2>
<p class = "error-msg">Error with Places API</p>`;
});
}();
但是我遇到了错误:SyntaxError: unexpected token: ':'
实际上,我尝试在dataType: 'json
方法中使用$.ajax()
',但它给出了另一个错误:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJH62rupQOGToRFiMvN5pH5m4&key=MY_API_KEY&_=1530809608880. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
然后我根据this thread
中的答案实施了该解决方案但仍然出现错误。