我有一个php文件(index.php),它调用另一个文件(ajax.php),它由javascript和ajax调用组成。
index.php,$mid
是从数据库中获取的变量:
<?php
$url = "http://localhost/parser/ajax.php?m=$mid";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_exec($ch);
ajax.php:
<?php
$mid = $_GET["mid"];
var r = {
feedLife: 60,
feedParams: {
competition: '129',
season: SEASON,
sport: SPORT,
match: <?php echo $mid ?>,
}
};
$.ajax({
url: 'upload_json.php?m=<?php echo $mid ?>',
dataType: 'json',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(a),
processData: false,
success: function(){
},
error: function( jqXhr, textStatus, errorThrown ){
console.log( errorThrown );
}
});
我想运行一个cron,但由于控制台无法解析javascript,我不知道如何实现自动化?有任何想法吗?也许phantomJS可以做一招?
提前致谢
答案 0 :(得分:0)
你不能在php文件中编写javascript
尝试使用NodeJs + Cron
答案 1 :(得分:0)
将所有javascript代码连接到字符串中,并将其换行并使用echo
<?php
$mid = $_GET["mid"];
$script = "<script> var r = {
feedLife: 60,
feedParams: {
competition: '129',
season: SEASON,
sport: SPORT,
match: '".$mid."',
}
};
$.ajax({
url: 'upload_json.php?m=".$mid."',
dataType: 'json',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(a),
processData: false,
success: function(){
},
error: function( jqXhr, textStatus, errorThrown ){
console.log( errorThrown );
}
});</script>";
echo $script;
答案 2 :(得分:-1)
你有错误,你需要在变量$ mid后关闭php标签。示例
<?php
$mid = $_GET["mid"];
?>