JSONP cannot use results coming from API

问题: I was trying this to run this ajax request to get a bunch of items and at least log them: in the console the items show up as undefined: but in the network tab I...

问题:

I was trying this to run this ajax request to get a bunch of items and at least log them:

enter image description here

in the console the items show up as undefined:

enter image description here

but in the network tab I can see the items that I was trying to call from API:

enter image description here

I am confused because neither error or success function is triggered.

src code:

<!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>

回答1:

Try dataType:JSON Your API is returning JSON, not JSONP.


回答2:

do you have cross domain problem? and you want try to use jsonp ?

  • 发表于 2019-02-19 21:54
  • 阅读 ( 165 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除