jQuery AJAX调用以获取Web API数据返回语法错误 [英] jQuery AJAX call to fetch web api data returns syntax error

查看:120
本文介绍了jQuery AJAX调用以获取Web API数据返回语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取此对象从这个网址:

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

由于它是希腊文,所以可能收效不大,但是您会得到格式.

这是我的代码:

function getDicts() {
  api_url = 'https://test3.diavgeia.gov.gr/luminapi/opendata/dictionaries/KANONISTIKI_PRAXI_TYPE.json'
  $.ajax({
    headers: {
      'Access-Control-Allow-Origin': '*',
    },
    url: api_url,
    type: "GET",
    crossDomain: true,
    dataType: 'jsonp',
    success: function(api_data) {
      var obj = $.parseJSON(api_data);
      console.log(obj);
    },
    error: function(error) {
      console.log(error);
    },

  });
};
};
getDicts();

如果我使用 as the dataType的问题`我得到了错误: SyntaxError:JSON.parse:数据

这是我通话的屏幕截图:

这里是一个小提琴: https://jsfiddle.net/danee/sz7tLru8/

更新!

在找到一些示例之后,我对功能进行了少许更改.在网络"标签上,使用 dataType的问题我确实得到了对象的问题,但在控制台上我得到这个: SyntaxError:缺少;声明前[了解更多] KANONISTIKI_PRAXI_TYPE.json:1:7

这是来自jsonp的通话的屏幕截图. 似乎在对象的问题上出现错误?以前没有遇到过.

我也更新了小提琴,但是我什么也没收到,也没有回应.

解决方案

您的Internet浏览器正在阻止CORS Ajax调用.一种解决方法是,在没有此限制的情况下,仅部署自己的Web应用程序以运行针对diavgeia.gov.gr外部Web api的Web请求.

Internet浏览器-(Ajax)->您的Web服务器->目标网站

另一种选择是使用已经免费提供此服务的反向代理网站.他们将从服务器而不是Internet浏览器为您调用外部Web api,从而可以避免CORS.

Internet浏览器-(Ajax)-> 反向代理->目标网站

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);
  }
});

如果Allorigins被您的互联网提供商(ISP)阻止,请尝试从 https://cors-anywhere.herokuapp.com

  • http://www.whateverorigin.org
  • http://anyorigin.com
  • 注意:根据您的帖子,我假设您无权对网络api进行更改.否则,可以从那里启用CORS或进行更改以返回jsonp而不是json.

    I am trying to fetch this object from this url:

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

    You might not get much since it's in greek but you get the format.

    Here's my code:

    function getDicts() {
      api_url = 'https://test3.diavgeia.gov.gr/luminapi/opendata/dictionaries/KANONISTIKI_PRAXI_TYPE.json'
      $.ajax({
        headers: {
          'Access-Control-Allow-Origin': '*',
        },
        url: api_url,
        type: "GET",
        crossDomain: true,
        dataType: 'jsonp',
        success: function(api_data) {
          var obj = $.parseJSON(api_data);
          console.log(obj);
        },
        error: function(error) {
          console.log(error);
        },
    
      });
    };
    };
    getDicts();
    

    If I use as thedataType` I get the error: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the data

    This is a screenshot from my call:

    Here's a fiddle : https://jsfiddle.net/danee/sz7tLru8/

    UPDATE!

    I changed slightly the function, following some examples I found. Using the dataType, on the Network tab I do get a object but on the console I get this: SyntaxError: missing ; before statement[Learn More] KANONISTIKI_PRAXI_TYPE.json:1:7

    Here's a screenshot from the call with jsonp. That seems like an error on the object? Haven't encountered that before.

    I have updated the fiddle as well, but I get nothing there, no response.

    解决方案

    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.

    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.

    这篇关于jQuery AJAX调用以获取Web API数据返回语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆