jQuery AJAX call to fetch web api data returns syntax error

后端 未结 1 1478
南旧
南旧 2021-01-07 12:41

I am trying to fetch this json object from this url:

https://test3.diavgeia.gov.gr/luminapi/opendata/dictionaries//KANONISTIKI_PRAXI_TYPE.json

You might not

1条回答
  •  走了就别回头了
    2021-01-07 13:20

    Your internet browser is blocking CORS Ajax calls. As a workaround one option is to deploy your own web application just for running web requests to target diavgeia.gov.gr external web api without this restriction.

    Internet browser -(Ajax) -> Your web server -> Target web site

    Another option is using reverse proxy websites that already provide this service for free. They will call the external web api for you from a server, not an Internet browser, so that CORS can be avoided.

    Internet browser -(Ajax) -> Reverse proxy -> Target web site

    let anonimizerUrl = 'https://allorigins.us';
    var apiUrl = 'https://test3.diavgeia.gov.gr/luminapi/opendata/dictionaries/KANONISTIKI_PRAXI_TYPE.json';
    $.ajax({
      url: anonimizerUrl + '/get?url=' + encodeURIComponent(apiUrl),
      success: function(response) {
        //Although response is an object, the field contents is a string and needs to be parsed here.
        var data = JSON.parse(response.contents); 
        console.log(data);
      },
      error: function(request) {
        console.log(request.responseText);
      }
    });
    

    If Allorigins is blocked by your internet provider (ISP), try another similar server from this list including.

    • https://cors-anywhere.herokuapp.com
    • http://www.whateverorigin.org
    • http://anyorigin.com

    Note: Based on your post, I'm assuming you don't have access to make changes to the web api. Otherwise CORS can be enable from there or make changes for returning jsonp instead of json.

    0 讨论(0)
提交回复
热议问题