使用JSONP时的CORB [英] CORB when using JSONP

查看:233
本文介绍了使用JSONP时的CORB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试JSONP.我有一个类似下面的HTML:

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

我的main.js文件是:

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

我的php文件是:

<?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.')';
            
  ?>

我将php文件托管在 https://truthsearcher83.000webhostapp.com/players_json.php

我的控制台出现此错误,而console.log没有显示任何内容.

跨域读取阻止(CORB)阻止了跨域响应 https://MIME类型为text/html的truesearcher83.000webhostapp.com/players_json.php?callback=showPlayers .有关更多详细信息,请参见 https://www.chromestatus.com/feature/5629709824032768 . >

我刚刚开始学习Ajax和JSONP,并且我了解JSONP可以处理这样的跨源请求.那么,为什么我会收到此错误?与服务器有关吗?

解决方案

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

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

与此帖子相关的

尝试在服务器端设置正确的MIME类型.另外,尝试在PHP中删除var_dump,这会复制脚​​本标记中的对象

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?

解决方案

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

这篇关于使用JSONP时的CORB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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