JSONP无法使用来自API的结果

问题描述 投票:1回答:2

我试图运行这个ajax请求以获取一堆项目并至少记录它们:

enter image description here

在控制台中,项目显示为undefined:

enter image description here

但是在网络标签中我可以看到我试图通过API调用的项目:

enter image description here

我很困惑,因为没有触发错误或成功功能。

src代码:

<!DOCTYPE html>
<html>
<head>
    <title>Hello World</title>
    <link rel="stylesheet" type="text/css" href="css/fontawesome-free-5.6.3-web/css/all.css">
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="./css/main.css">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body>

</body>

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>


<script>
    console.log("alive");

      $.ajax({
                type: "GET",
                url: "http://127.0.0.1:3000/api/stories?callback=items",
                dataType: 'jsonp',
                jsonpCallback: 'items',
                jsonp: 'callback',
                success: function(data) {
                    var strData = JSON.stringify(data);
                    console.log(strData);
                },
          error: function(xhr,status,error) {
                    console.log(status);
                    console.log(error);
          }
      });


    console.log(items());

    </script>
</html>
jquery json ajax jsonp
2个回答
0
投票

尝试dataType:JSON您的API返回JSON,而不是JSONP。


0
投票

你有跨域问题吗?你想尝试使用jsonp吗?

© www.soinside.com 2019 - 2024. All rights reserved.