CORS cross origin resource errors - Javascript/JQuery
Copy link to clipboard
Copied
Im encountering CORS cross origin errors when trying to perform a GET request to an API on another server. I'm fairly new to coding and am trying to build a vehicle registration lookup which gets data from another server. I presumed I could build this and preview it in dreamweaver before adding this feature to my website.
I tried XMLhttprequest, javascript and JQuery all receiving the same CORS errors Is there any way to get this to work in live preview. Below is an example of my script.
$.ajax({
url: $("#apicall").text,
type: "GET",
crossdomain: false,
datatype: "jsonp",
success: function(data,textstatus,xhr){
$("#div1").load(data)
console.log(data);
},
Thanks in advance
Copy link to clipboard
Copied
For security reasons, this would be a typical response when attempting to access data & scripts from another domain.
http://www.ajax-cross-origin.com/how.html
Copy link to clipboard
Copied
I took a look at that page, it appears to say using jsonp is a workaround for cross origin errors, and if you look at my script I am using jsonp or am I misinterpreting.
Copy link to clipboard
Copied
Are you paying for your service?
http://www.vehicleregistrationapi.com/
Copy link to clipboard
Copied
sorry ... it's pretty old, and I don't know if it is free access, or premium, but you can give it a try
in any case I should be able to unlock it some how... at least I should ask for that precise title if needed
Copy link to clipboard
Copied
Are you running a local server on your computer?
You cant usually get anything from an API unless youre either running the webpage using a local server like Xampp or Mamp or your editor has the ability to create a local server environment without the necessity to install a dedicated one.
Also is this a public API or one which requires authentication?
Copy link to clipboard
Copied
yes good catch... 😉
Copy link to clipboard
Copied
People have kind of got things in here but to be clearer..
CORS
- You can not just tap into another sites data unless allowed to do so.
- Depending on the server setup you may either need an API key to access data or white list permission. CORS can be an output response but even JSONP vs JSON if the server is setup to deny the access then you have to talk to that vendor/3rd party in regard to how you access.
- JSONP is not JSON. While it has JSON data it is not a pure JSON data response and normally wrapped into a function so how you call it is different.
- If you are doing a sollution locally various things like fonts, AJAX requests and so on will not function and MAC is more locked down than on a PC as well in that regard so doing such requests will always fail. There are hacks/mods to windows etc to get various things in this regard working but they are all extra steps to get something running.
Copy link to clipboard
Copied

