控制台最终回写
未捕获的ReferenceError:未定义provhtml
此外,当我将我的Javascript更改为下面的代码时,控制台最终只会写&#34; <?= $provhtml ?>
&#34;
var provnum = "<?= $provhtml ?>";
console.log(provnum);
我不确定问题是什么。当用户点击名为otherlist的按钮时,我希望$ provhtml写在控制台内
(我也不确定是否需要d.preventDefault();
这是我的prov.php
<?php
$phonenum = $_POST["phonenum"];
$provhtml = file_get_contents('http://api.data24-7.com/v/2.0?user=USERNAME&pass=PASSWORD&api=C&p1=1' . $phonenum);
?>
以下是我的index.html中的脚本
$('button[name="otherlistbut"]').click(function(d) {
lookupProvider(d);
});
});
/************ FUNCTIONS ******************/
function lookupProvider(d) {
d.preventDefault();
var phonenum = $('input[name="phonenum"]').val();
$.ajax({
type: 'POST',
data: {
phonenum: phonenum
},
url: 'prov.php',
success: function(data) {
var provnum = $(provhtml);
console.log(provnum);
},
error: function(xhr, err) {
console.log("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
console.log("responseText: " + xhr.responseText);
}
});
}
答案 0 :(得分:0)
在prov.php文件中
<?php
$phonenum = $_POST["phonenum"];
$provhtml = file_get_contents('http://api.data24-7.com/v/2.0?user=USERNAME&pass=PASSWORD&api=C&p1=1' . $phonenum);
echo json_encode( array( "phonenum" => $phonenum,"provhtml"=>$provhtml)); //You should be returning the values to your ajax function
?>
在index.html中
function lookupProvider(d) {
d.preventDefault();
var phonenum = $('input[name="phonenum"]').val();
$.ajax({
type: 'POST',
data: {
phonenum: phonenum
},
url: 'prov.php',
success: function(data) { //Receives the data from the php code
var provnum = data.phonenum;
console.log(provnum);
},
error: function(xhr, err) {
console.log("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
console.log("responseText: " + xhr.responseText);
}
});
}