CORB when using JSONP

帅比萌擦擦* 提交于 2021-01-29 00:14:25

问题


I am trying JSONP. I have a HTML like below:

<html>
<head>
<title>JSONP</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
    
    <script src="js/main.js"></script>
    <script src ="https://truthsearcher83.000webhostapp.com/players_json.php?callback=showPlayers"></script>
    
</body>
</html>

My main.js file is :

function showPlayers(data){
    console.log(data);
}

My php file is :

<?php

$json_obj = '{

    $json_obj = '{"sachin" :{"country":"India" ,"age":36 , "role":"bat"},'.
    '"sourav" :{"country":"India" ,"age":37 , "role":"bat"},'.
    '"pointing" :{"country":"Aus" ,"age":56 , "role":"bowl"},'.
    '"gilchrist" :{"country":"Aus" ,"age":16 , "role":"wick"}}';
echo var_dump($json_obj);
echo 'showPlayers('.$json_obj.')';
            
  ?>

I am hosting the php file at https://truthsearcher83.000webhostapp.com/players_json.php

I am getting this error in my console and my console.log is not showing anything .

Cross-Origin Read Blocking (CORB) blocked cross-origin response https://truthsearcher83.000webhostapp.com/players_json.php?callback=showPlayers with MIME type text/html. See https://www.chromestatus.com/feature/5629709824032768 for more details.

I have just started learning Ajax and JSONP and I understand JSONP takes care of Cross Origin requests like this. So why am I getting this error? Is it something to do with the server?


回答1:


https://stackoverflow.com/a/24528549/9265743

header('Access-Control-Allow-Origin: *');
header('Content-Type: application/javascript');

with reference to this post try setting correct MIME type in the server side. Also, try removing the var_dump in PHP, this duplicates the object in the script tag



来源:https://stackoverflow.com/questions/54567889/corb-when-using-jsonp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!