简体   繁体   English

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

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

I'm currently writing a program, that should convert all addresses from a database to its corresponding lat/long coordinates. 我正在编写一个程序,它应该将数据库中的所有地址转换为相应的纬度/经度坐标。 For this I am using the HERE Geocoding API. 为此,我使用的是HERE地理编码API。 Since this program is meant for multiple people I let them enter their app_id and app_code manually. 由于此程序适用于多个人,我让他们手动输入app_id和app_code。 The problem is, that if one of the credentials is invalid it will return XML instead of JSON, which causes an error: (SyntaxError: expected expression, got '<') 问题是,如果其中一个凭据无效,它将返回XML而不是JSON,这会导致错误: (SyntaxError: expected expression, got '<')

This is an example link, which gets called by the geocoder: 这是一个示例链接,由地理编码器调用:

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) 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 )

I tried to somehow access the error, but since there's already an error inside the Geocoder I have to wait for the timeout (30s) for my "onError" callback to be called. 我试图以某种方式访问​​错误,但由于Geocoder中已经存在错误,我必须等待超时(30s)才能调用我的“onError”回调。 And even then I don't get an error-code like "invalidCredentials", but just an Object with line and column number. 即便如此,我也没有得到像“invalidCredentials”这样的错误代码,而只是一个带有行号和列号的对象。

Basically I'm just using the normal geocoding service 基本上我只是使用普通的地理编码服务

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

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

I don't think this works as intended. 我认为这不符合预期。 The Geocoder.geocode() function is expecting a JSON value to pass it on to the callback function, but it's failing since it can't parse XML Data to a JSON object. Geocoder.geocode()函数期望JSON值将其传递给回调函数,但它失败了,因为它无法将XML数据解析为JSON对象。

If this is working as intended, I would be happy about an advice on how to handle this problem, or maybe a solution to test the validity of given tokens. 如果这是按预期工作的,我会很高兴有关如何处理这个问题的建议,或者可能是测试给定令牌的有效性的解决方案。

It is because the actual error (PermissionError) is wrapped as xml object. 这是因为实际错误(PermissionError)被包装为xml对象。 In you onError function you have to handle the xml object. 在onError函数中,您必须处理xml对象。

Below is the sample error object: 以下是示例错误对象:

<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