jquery - 尝试获取工作结果在 Postman 中完美运行,但在浏览器中使用 AJAX 需要 app_code 和 app_id
问题描述
我正在尝试使用 HERE 作业 REST API 来获取我的作业结果。我在 Postman 中测试了我的 GET 请求,它使用我的 apiKey 完美运行,无需使用任何额外的标头(Content-Type: application/octet-stream 除外)。
但是当我尝试使用 AJAX 和 jQuery 尝试相同的请求时,我收到 400 响应,告诉我我需要 app_id 和 app_code。 我的代码:
$.ajax({
async: false,
contentType: "application/octet-stream",
type: 'GET',
url: "https://batch.geocoder.ls.hereapi.com/6.2/jobs/{JOB_ID}/result?apiKey={API_KEY}",
}).done(function (result) {
console.log(result);
});
返回标头:
{"error":"Bad Request","error_description":"The request is missing the app_id and app_code parameters. They must both be passed as query parameters. If you do not have app_id and app_code, please obtain them through your customer representative or at http://developer.here.com/myapps."}
将其放入浏览器的 URL 栏中时,使用此普通链接确实有效。
使用REST 下https://developer.here.com/projects/页面上的应用程序 ID 和 apikey 作为 api_code 的组合也不起作用。这告诉我这是一个无效的对。
我不知道我现在应该实际使用什么凭据。
我在这里先向您的帮助表示感谢!
解决方案
根据文档,您必须使用apikey
进行身份验证。请检查此代码是否正常工作的 ajax。
<html>
<button type="button" class="btn">Click me!</button>
<p class="text">Replace me!!</p>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script>
$('.btn').click(function() {
$('.text').text('loading . . ');
$.ajax({
type:"GET",
url:"https://batch.geocoder.ls.hereapi.com/6.2/jobs/CxnGVvxLZeCjJnGRD0udKgLgeQzNa5xq/result?apiKey={apikey}",
success: function(result) {
$('.text').text(JSON.stringify(result));
console.log(result);
},
});
});
</script>
</html>
希望这可以帮助。