Skip to main content
Participant
June 20, 2021
Question

CORS cross origin resource errors - Javascript/JQuery

  • June 20, 2021
  • 5 replies
  • 933 views

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

    This topic has been closed for replies.

    5 replies

    Liam Dilley
    Inspiring
    June 24, 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.

     

    B i r n o u
    Legend
    June 24, 2021
    Legend
    June 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?

    B i r n o u
    Legend
    June 21, 2021

    yes good catch... 😉

    B i r n o u
    Legend
    June 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-cross-domain?resume=false

    Nancy OShea
    Community Expert
    Community Expert
    June 20, 2021

    Are you paying for your service?

    http://www.vehicleregistrationapi.com/

     

    Nancy O'Shea— Product User & Community Expert
    Nancy OShea
    Community Expert
    Community Expert
    June 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
    Participant
    June 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.