我遇到了以下问题,下面的脚本发送一个关键字到另一个域中的PHP文件(我已经添加了CROS头文件),这个PHP返回一些"echos of different variables"
(标题,缩略图,url等)。它可以工作,但会随机返回"Undefined variables"
。
第一件事是在PHP中添加一个if (isset ())
到我的变量中,错误不再出现,但我的搜索返回的结果要小得多(添加它之前平均有10到20个结果,现在我得到5个结果)。
这会是我的脚本有问题吗?
我的form.php
<form method="POST" action="" id="form-busqueda">
<input type="text" name="keyword">
<button id="search" name="search">Search</search>
<div id="results"></div>
<script>
jQuery(function($){
var pluginUrl = '<?php echo plugin_dir_url( __FILE__ ); ?>' ;
$('[id^="form-busqueda"]').on('submit', function(e) {
e.preventDefault();
$.ajax({
type : 'POST',
url : 'http://localhost/ladoserver/script.php',
data : $(this).serialize(),
beforeSend: function(){
$('#results').html('<img src="'+pluginUrl+'../../assets/img/loading.gif" />');
}
}).done(function(data) {
$('#results').html(data);
});
});
});
</script>
</form>
My script.php
(dlPage是一个创建cURL连接的函数):
<?php
if (isset($_POST['keyword'])) {
$search = $_POST['keyword'];
$html = dlPage("http://example.com/" . $search);
//where I search and get with simple_html_dom example:
$video = $videos->find('div.example2>a', 0);
$title = $video->innertext;
$url = $video->attr['href'];
$id = $video->attr['id'];
$thumbnail = $video->find('div.thumb', 0)->innertext;
echo $title;
echo $url;
echo $id;
echo $thumbnail[0];
}
?>
我已经更新了我的代码,我没有把所有的代码放进去,因为我认为它不相关,我的script.php
在纯PHP下工作得很好。当我使用AJAX时,问题就出现了。
我得到以下错误:
通知:未定义的变量: C:\xampp\htdocs\webs\ladoserver\script.php中第13行的标题
注意:未定义的变量:第13行的C:\xampp\htdocs\webs\ladoserver\script.php中的标题
注意:未定义的变量:第14行上的C:\xampp\htdocs\webs\ladoserver\script.php中的url
https://stackoverflow.com/questions/44574904
复制相似问题