I explain my drawback: I am trying to consume a API of a page so I make the following request:
<script>
var button = document.getElementById("btn_submit");
button.addEventListener("click", e => {
e.preventDefault;
var vin = document.getElementById("id_vin").value;
console.log(vin);
var url = 'https://api.mercedes-benz.com/image/v1/vehicles/${vin}/components?apikey=mi_clave';
$.ajax({
url: url,
type: 'GET',
accepts: "application/json",
crossDomain: true,
success: function (result) {
// process result
$('#result').html(result.ip);
},
error: function (e) {
// log error in browser
console.log(e.message);
}
});
})
</script>
But I get the following error:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
For what I read, how to add the header or something like that is my first time working with AJAX so they can guide me, I would really appreciate it.
Greetings: D
UPDATE.
var url = 'https://api.mercedes-benz.com/image/v1/vehicles/${vin}/components?apikey=xxxx';
$.ajax({
url: url,
type: 'GET',
accept: "application/json",
crossDomain: true,
dataType: 'jsonp',
success: function (result) {
// process result
$('#result').html(result.ip);
},
error: function (e) {
// log error in browser
console.log(e.message);
}
});
})
This gives me the following error:
But if I click on the link I can see the result then I do not know how to treat that result so that it prints on the page:
UPDATE 2
when modifying the dataType: "json"
I get the following error:
And here's the answer from the Network tab> Hearders
This is the preview of the answer (what I want)
And the answer in json seems to me:
I clarify that my page is hosted locally, that is, I have not uploaded it to any server, I am running it from my local laptop