[请益] Ajax与PHP Simple HTML DOM Parser

楼主: SpringMud (呆呆。)   2015-03-10 18:33:22
请教各位大大,
小弟最近使用PHP Simple HTML DOM Parser去parse <a>里的href,
我这样写可以正确跑出我预期的结果。
以下是我的code:
*File Name: parser.php
====================================================================
include( 'simple_html_dom.php' ); //PHP Simple HTML DOM Parser file
$msg = '<div><p><a href="http://www.apple.com"></a></p></div>';
$html = str_get_html($msg);
$temp = "/123";
$html->find('a',0)->href = 'http://www.google.com'.$temp;
echo $html->find('a',0)->href; //OUTPUT => http://www.google.com/123
====================================================================
但是,如果我用ajax将index.php的变量丢到parser.php来处理,就会出现问题。
*File Name: index.php
====================================================================
<div id="test_area">
<div><p><a href="http://www.apple.com"></a></p></div>
</div>
<button id="send_btn" type="button">Send</button>
<script>
$(document).ready(init);
function init(){
$('#send_btn').click(function(){
var message = $('#test_area').html();
$.ajax(
{
url: 'parser.php',
cache: false,
dataType: 'json',
type: "POST",
data: {message_ajax:message},
error: function(xhr) {
alert('ERROR');
},
success: function(response)
{
alert("Works!");
}
});
});
}
</script>
====================================================================
然后小改一下parser.php
*File Name: Parser.php
====================================================================
include( 'simple_html_dom.php' ); //PHP Simple HTML DOM Parser file
$msg = $_POST['message_ajax']; //改了这里!!!
$html = str_get_html($msg);
$temp = "/123";
$html->find('a',0)->href = 'http://www.google.com'.$temp;
====================================================================
最后我去看<a>里的href,竟然不会动,
依旧是原来的"http://www.apple.com",
而不是"http://www.google.com/123"。
求解!!!(跪
作者: anest (形状最重要大小不能少)   2015-03-10 19:21:00
1.有跳出works的视窗吗?2.你没处理收到的资料啊.....
楼主: SpringMud (呆呆。)   2015-03-10 19:30:00
我用ajax POST data,并且用$_POST接资料
作者: Darkland (小黑XDrz)   2015-03-10 22:12:00
你的 js 有处理 parser.php echo 出来的结果吗?
楼主: SpringMud (呆呆。)   2015-03-11 01:10:00
作者: Darkland (小黑XDrz)   2015-03-11 21:11:00
但你贴出来的部份并没有 js 最后处理的方式?
作者: xzeertacat (BlueCat)   2015-03-13 00:42:00
把$msg印出来 看收到什么

Links booklink

Contact Us: admin [ a t ] ucptt.com