我正在使用 php 中的curl 检索页面。因此,在检索到的页面中,我想使用 jQuery 中的append() 方法添加一个新元素。我可以在我托管的网站上执行此操作。但在其他知名网站上却不起作用。为什么它不起作用?
php代码
<?php
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "http://www.example.com");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// $output contains the output string
$output = curl_exec($ch);
echo $output;
if(curl_exec($ch) === false)
{
echo 'Curl error: ' . curl_error($ch);
}
// close curl resource to free up system resources
curl_close($ch);
?>
这是我的 jQuery 和 Ajax 脚本
<html>
<head>
<script src="jquery-1.11.3.js"></script>
</head>
<body>
<script>
$(document).ready(function() {
$.ajax({
type: "GET",
url: "http://localhost:8080/curlstart.php",
success: function(data, textStatus ){
alert(data);
$("body").append(data);
$(document.body).append("<p>Hello</p>");
},
error: function(xhr, textStatus, errorThrown){
alert("request failed in next year="+errorThrown);
}
});
});
</script>
</body>
</html>
最佳答案
检查您的url:“http://localhost:8080/curlstart.php”,
——您似乎正在尝试在托管服务器上访问本地主机。您可以将 http://localhost:8080/curlstart.php
替换为 curlstart.php
。
关于javascript - 附加在从 php 中的curl 检索到的页面上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31545460/