无法使用JS

时间:2017-10-03 18:43:39

标签: javascript jsonp

我链接到下面引用的Feed,我想仅使用JavaScript在HTML文件中显示其元素的数据。我首先尝试在控制台中进行此操作但出现错误无效的JSON格式

任何想法如何解决这个问题以及如何使其发挥作用?提前谢谢。

$(document).ready(function() {
  var link = 'https://neon.epson-europe.com/frame/interfaces/newsroomcustom.php?json={"area":["blogarticle"],"tags":["Kids%20corner"],"limit":3,"offset":0,"language":"en","country":"GB","action":"getList","jsonp":"jsonp"}';
  $.ajax({
    dataType: 'jsonp',
    data: 'id=10',
    url: link + ";callback=?",
    success: function(data) {
      console.log(data);
    },
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>TestTest</title>
  <script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous"></script>
</head>

<body>

  <h1>article title should be here</h1>
  <p> subtitle should be here </p>
  <image src=" link to the image should be here ">
    <p>date should be here</p>


</body>

1 个答案:

答案 0 :(得分:0)

我做了一些更改,以使您的JS更具可读性,无论如何使这一切成为可行的是我添加的这个属性:jsonpCallback: 'jsonp'。以下是修改后的完整代码:

$(document).ready(function() {
  var link = 'https://neon.epson-europe.com/frame/interfaces/newsroomcustom.php';
  $.ajax({
    dataType: 'jsonp',
     jsonpCallback: 'jsonp',
    data: 'json={"area":["blogarticle"],"tags":["Kids%20corner"],"limit":3,"offset":0,"language":"en","country":"GB","action":"getList","jsonp":"jsonp"}&id=10',
    url: link + ";callback=?",
    success: function(data) {
      console.log(data);
    },
  });
});

测试并告诉我是否适合你。