我试图将一些数据从PHP发送回AJAX。我找到了一些例子,但它似乎没有用。控制台日志的结果是:"测试:成功"。我怎样才能获得数据?
$.ajax({
url: "assets/psv.php",
method: "POST",
dataType: "HTML",
success: function(results, test){
console.log("test:" + test);
},
error : function (e) {
console.log("error " + e);
}
});
PHP
$test= "pgv";
echo $test;
答案 0 :(得分:2)
尝试:
$.ajax({
url: "assets/psv.php",
method: "GET",
success: function(data){
console.log("test:" + data);
},
error : function (e) {
console.log("error " + e);
}
});
或类似的东西:
$.get( "assets/psv.php", function( data ) {
alert( "Data Loaded: " + data );
});
答案 1 :(得分:0)
成功回调中的第一个变量包含从您调用的页面接收的数据。
<强> PHP 强>
header('Content-Type: application/json');
$test= "pgv";
echo json_encode($test);
JS
$.ajax({
url: "assets/psv.php",
method: "GET",
dataType: "json",
success: function(response, status){
console.log("test", response.data); //CHANGE THIS!!
},
error : function (e) {
console.log("error " + e);
}
});
数据将包含
{
headers: "...",
data: "pgv",
....
}