Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

CORS cross origin resource errors - Javascript/JQuery

New Here ,
Jun 20, 2021 Jun 20, 2021

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

714
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 20, 2021 Jun 20, 2021

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

 

 

Nancy O'Shea— Product User, Community Expert & Moderator
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 20, 2021 Jun 20, 2021

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. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 20, 2021 Jun 20, 2021

Are you paying for your service?

http://www.vehicleregistrationapi.com/

 

Nancy O'Shea— Product User, Community Expert & Moderator
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 21, 2021 Jun 21, 2021

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

https://www.linkedin.com/learning/ajax-et-jquery-utilisation-des-donnees-dynamiques/jsonp-ou-le-cros...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 21, 2021 Jun 21, 2021

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 21, 2021 Jun 21, 2021

yes good catch... 😉

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 23, 2021 Jun 23, 2021

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.

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 23, 2021 Jun 23, 2021
LATEST
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines