douxiluan6555 2013-04-04 21:50
浏览 45

以JSON格式从PHP文件获取内容回到javascript

When sending the request from the jQuery Mobile script to the specified PHP file, nothing is returned, nothing is appended to the html file. Here's the URL of the page:

localhost/basket/newstext.html?url=http://www.basket-planet.com/ru/news/9235

newstext.html:

<head>
<script src="js/newstext.js"></script> 
</head>
<body> 
<div data-role="page" id="newstext"> 
  <div data-role="content"> 
    <div id="textcontent"></div> 
    </div> 
  </div> 
</body> 

newstext.js:

var serviceURL = "http://localhost/basket/services/"; 
$('#newstext').bind('pageshow', function(event) { 
var url = getUrlVars()["url"]; 
$.getJSON(serviceURL + 'getnewstext.php?url='+url, displayNewsText); 
}); 

function displayNewsText(data){ 
  var newstext = data.item; 
  console.log(newstext);
  $('#textcontent').text(newstext); 
  $('#textcontent').trigger('create'); 
} 

function getUrlVars(){ 
//it displays in the alert perfectly, shortening the message here 
} 

getnewstext.php:

<?php 
include_once ('simple_html_dom.php'); 
$url = $_GET['url']; 
$html = file_get_html(''.$url.''); 

$article = $html->find('div[class=newsItem]'); 
$a = str_get_html(implode("
", (array)$article)); 
//parse the article 
header("Content-type: application/json");
echo '{"item":'. json_encode($a) .'}';
?>

I think my problem is how I'm encoding the $a variable in the PHP script. The $a variable contains html tags of all kind...how can I append it in the html file?

  • 写回答

1条回答 默认 最新

  • doufen5175 2013-04-04 21:56
    关注

    Where you have this line:

    $.getJSON(serviceURL + 'getnewstext.php?url='+url, displayNewsText);
    

    Change it to be:

    $.getJSON(serviceURL + 'getnewstext.php?url='+url, displayNewsText, function(response){
        $('#elem').append(response);
    });
    

    Where #elem is the name of the element that you want to append the data, returned from the PHP file, to.

    评论

报告相同问题?

悬赏问题

  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法