javascript - 附加在从 php 中的curl 检索到的页面上不起作用

标签 javascript php jquery ajax curl

我正在使用 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/

相关文章:

javascript - 使用 remote = true 从 JS 更新 Rails View

javascript - 主干点击事件在 Firefox 中无法正常工作

javascript - 句子中间的省略号

javascript - Contenteditable 元素修改后会更改 ID

javascript - html、javascript 和 php 的非 CSS 样式注意事项

php - 使用 SQL Select 语句更新临时表的问题

javascript - Chrome 扩展触发 Keydown JS

javascript - 从 Action 方法调用外部 Javascript

javascript - Angular 6 + Ngrx 状态值更新而不使用操作

php - Laravel 插入多个表和多个数据