繁体   English   中英

使用Geocoder.geocode()时,使用无效凭据返回xml而不是json

[英]Using invalid credentials returns xml instead of json when using Geocoder.geocode()

我正在编写一个程序,它应该将数据库中的所有地址转换为相应的纬度/经度坐标。 为此,我使用的是HERE地理编码API。 由于此程序适用于多个人,我让他们手动输入app_id和app_code。 问题是,如果其中一个凭据无效,它将返回XML而不是JSON,这会导致错误: (SyntaxError: expected expression, got '<')

这是一个示例链接,由地理编码器调用:

http://geocoder.api.here.com/6.2/geocode.json?xnlp=CL_JSMv3.0.17.0&app_id=invalidCredentialID&app_code=InvalidCredentialCode&street=Haupstr.%2042&postalCode=99880&city=Leina&country=DE&jsoncallback=H.service.jsonp.handleResponse(0 )

我试图以某种方式访问​​错误,但由于Geocoder中已经存在错误,我必须等待超时(30s)才能调用我的“onError”回调。 即便如此,我也没有得到像“invalidCredentials”这样的错误代码,而只是一个带有行号和列号的对象。

基本上我只是使用普通的地理编码服务

var platform = new H.service.Platform({
  app_id: 'invalidCredentialId',
  app_code: 'invalidCredentialCode'
});
var geocoder = platform.getGeocodingService();

geocoder.geocode({searchText: 'Berlin'}, handleResponse, handleError);

我认为这不符合预期。 Geocoder.geocode()函数期望JSON值将其传递给回调函数,但它失败了,因为它无法将XML数据解析为JSON对象。

如果这是按预期工作的,我会很高兴有关如何处理这个问题的建议,或者可能是测试给定令牌的有效性的解决方案。

这是因为实际错误(PermissionError)被包装为xml对象。 在onError函数中,您必须处理xml对象。

以下是示例错误对象:

<ns2:Error xmlns:ns2="http://www.navteq.com/lbsp/Errors/1" type="PermissionError" subtype="InvalidCredentials"><Details>invalid credentials for invalidCredentialId</Details></ns2:Error>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM