jQuery ajax 将数据从 PHP 提取到 jQuery 变量中


jQuery ajax fetch data from PHP into a jQuery variable

我需要使用 ajax 和 php 和 jquery 接收投票状态。以下是我的代码:

var VoteStatus= GetStatus() ;
var ID = $('#ID').val();
function GetStatus() {
    var res = '';
    $.ajax({
        type: "POST",
        data: {VoteID:ID} ,
        url: "/vote_status.php",
        async: false,       
        success: function(result) { res=result; } 
    });                  
    return res;
}
alert('Vote Status= ' + VoteStatus);

在我的 php 文件中:

$VoteID = $_POST['VoteID'];
$Property = $_POST['Property'];

if ( $VoteID == 0 ) 
    echo 'No Vote provided - Property = '. $Property;
exit;

警报框显示:投票状态 = 未提供投票

请帮忙。

我已经发布了VoteID,但php文件似乎没有收到它。

在此处尝试警报并检查其是否有效

 $.ajax({
        type: "POST",
        data: {"VoteID":ID} ,
        url: "/vote_status.php",
        async: false,       
        success: function(result) { 
  alert(result); } 
    });   

POST 变量的名称需要用引号引起来,如

data: {"VoteID":ID}

试试这个并检查 jquery ajax 手册

$.ajax({
    type: "POST",        
    data:"VoteID=" + ID +"&secondparam=" + secondvalue,
    url: "/vote_status.php",
    async: false,       
    success: function(result) { alert(result); } 
});