【发布时间】:2013-04-04 21:50:26
【问题描述】:
当从 jQuery Mobile 脚本向指定的 PHP 文件发送请求时,不会返回任何内容,也不会将任何内容附加到 html 文件中。这是页面的网址:
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("\n", (array)$article));
//parse the article
header("Content-type: application/json");
echo '{"item":'. json_encode($a) .'}';
?>
我认为我的问题是我如何在 PHP 脚本中对 $a 变量进行编码。 $a 变量包含各种 html 标记...如何将其附加到 html 文件中?
【问题讨论】:
-
确保在回显内容之前设置了内容标题,
header("Content-type: application/json");它可能就像 jquery 一样简单/您的浏览器不知道如何处理返回的文本块 -
还可以尝试对 displayNewsText 函数内的数据进行 console.debug 以查看该变量中的格式。您需要打开浏览器的控制台(咨询 google)才能查看输出
-
我更新了文本...你能检查一下我是否正确更新了吗?对于newstext.html,控制台中的响应是空的......在另一个页面上,我从mysql查询中获取数据,响应是带有数据的正确php文件。此页面的响应为空...
标签: php jquery json jquery-mobile