我一整天都在为我的答案寻找答案......我有一个 ajax 帖子,它将变量值发送到 php 页面。我认为根据成功弹出窗口,ajax 帖子工作正常,但我无法在 php 上获得值......
这是 ajax 帖子的示例:
var value = 150;
/*$.post('index.php',{variable:essai});*/
/*alert("Token:"+token);*/
$.ajax({
url:'index.php',
type:'POST',
data:'data1='+value,
success : function(data)
{
alert("Succès envoi de la donnée!");
},
error : function(resultat,statut,erreur)
{
alert("Erreur d'envoi de la donnée!");
},
complete : function(resultat,statut){
}
});
对于 php 示例:
<div id="right-frame">
<?php include("get.php");
if (isset($_POST['data1']))
{
$variable = $_POST['data1'];
}
?>
<p class="text">Données de la borne :</p>
<table><tr>
<th bgcolor="9fbcdc"><p class="titre">Info 1</p></th>
<th bgcolor="9fbcdc"><p class="titre">Info 2</p></th>
<th bgcolor="9fbcdc"><p class="titre">Info 3</p></th>
<th bgcolor="9fbcdc"><p class="titre">Info 4</p></th>
</tr>
<tr>
<th><?php echo $variable; ?></th>
<th><?php echo $variable2; ?></th>
<th><?php echo $variable3; ?></th>
<th><?php echo $variable4; ?></th>
</tr></table>
</div>
谢谢
您正在以data:'data1='+value
的形式发送数据
尝试像这样发送:
data: {
'data1': value
}
替换
data:'data1='+value
跟
data:{'data1':value}
从jquery/ajax的官方文档中,它说数据归档需要键/值对对象。
要发送到服务器的数据。如果还不是字符串,则会将其转换为查询字符串。它被附加到 GET 请求的 url 中。请参阅进程数据选项以防止此自动处理。对象必须是键/值对。如果 value 是数组,jQuery 会根据传统设置的值序列化具有相同键的多个值(如下所述)