我正在尝试通过curl请求加载JS文件并将其加载到我的应用程序中。
我有类似
的东西loadJS.php
$c = curl_init('http://partnerSite.com/test.js');
curl_setopt($c, CURLOPT_VERBOSE, 1);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$jsFile = curl_exec($c);
//I have tried this, give me error
echo $jsFile;
//I have tried this and give me error too.
header('Content-Type: application/json');
echo json_encode($jsFile);
curl_close($c);
在我的JS中
var url = '/loadoutSideJS'; redirect to loadJS.php
$http({
url:url,
method:'GET',
dataType:'json'
}).success(function(data) {
$.getScript(data);
})
我在控制台中收到414 request-url too large错误
由于某种原因,响应被视为纯文本而不是js文件。反正我是否可以将响应视为JS来加载外部JS文件?非常感谢!
更新
loadJS.php
$c = curl_init('http://partnerSite.com/test.js');
curl_setopt($c, CURLOPT_VERBOSE, 1);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$jsFile = curl_exec($c);
//I have tried this and give me error too.
header('Content-Type: application/javascript');
echo $jsFile;
curl_close($c);
在我的JS中
var url = '/loadoutSideJS'; redirect to loadJS.php
$http({
url:url,
method:'GET'
}).success(function(data) {
$.getScript(data);
})
答案 0 :(得分:1)
为什么不
<?php
header('Content-Type: application/javascript');
echo file_get_contents('http://partnerSite.com/test.js');