如何在$ .ajax中动态传递POST数据。我将从我的几个php页面调用此函数。所以这里的POST数据需要动态添加。这可能吗?
$.ajax({type: 'POST',
url: 'page.php',
data:({ **need to pass dynamically**}),
beforeSend:function(){
.....
}),
success: function(){
.....
})
});
答案 0 :(得分:1)
$.ajax({
type: 'POST',
url: 'page.php',
data:({
"username" : <?php echo $username;?>,
"password" : <?php echo $password;?>
}),
beforeSend:function(){
*/.....*/
}),
success: function(){
/*...........*/
})
});
或者只是建立自己的查询:
<?php
$data = array( 'foo'=>'bar',
'baz'=>'boom',
'cow'=>'milk',
'php'=>'hypertext processor'
);
$query = http_build_query($data, '', '&');
?>
url: 'page.php?' <?php echo $query;?>