最近在做东西的时候踩了挺多坑,所以记录下

前端:angular4

后端:thinkphp5

前端代码

submit()
  {
    var url = "http://域名/jtest/public/index/index/call?callback=JSONP_CALLBACK";

    this.jsonp.get(url).subscribe(
      function(data){
        alert(data);
        console.log(data);
      },
    function(err){
      alert("1");
      console.log(err);
    });
    
  }

后端代码

<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
class Index extends Controller
{
    
    public function call()
    {
        return json(['msg'=>'success','status'=>1]);
    }
}

报错信息1:JSONP injected script did not invoke callback

[angular4] jsonp跨域请求报错 "JSONP injected script did not invoke callback."解决

解决办法

在确认前端代码没问题的情况***意引入HttpModule和JsonpModule)。。。修改后端代码为

<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
class Index extends Controller
{
   
    public function call()
    {
       $arr = array ('msg'=>'success','status'=>1); 
    	echo $_GET['callback']."(".json_encode($arr).")";
    }
}

[angular4] jsonp跨域请求报错 "JSONP injected script did not invoke callback."解决

关于jsonp的跨域资料可以在下面两篇博文找到

https://blog.csdn.net/zhongxiucheng/article/details/7609732

https://blog.csdn.net/qq_36374223/article/details/54839502



报错信息2:callback=__ng_jsonp__.__req0.finished net::ERR_ABORTED

[angular4] jsonp跨域请求报错 "JSONP injected script did not invoke callback."解决

解决办法:

同样的先看下angular有没有问题(有没有使用jsonp方式发送请求或者有没有加callback的回调),然后查看后端,找到项目目录

[angular4] jsonp跨域请求报错 "JSONP injected script did not invoke callback."解决

我用的是tp5的框架(其他没试过),将runtime的权限改为777

[angular4] jsonp跨域请求报错 "JSONP injected script did not invoke callback."解决

然后重新发起请求就可以取得数据了

[angular4] jsonp跨域请求报错 "JSONP injected script did not invoke callback."解决

 

相关文章:

  • 2021-06-14
  • 2021-06-06
  • 2021-08-07
  • 2022-12-23
猜你喜欢
  • 2021-07-14
  • 2021-10-23
  • 2021-06-27
  • 2021-06-15
  • 2021-09-22
  • 2021-09-19
相关资源
相似解决方案