Javascript 当我尝试从Ajax发布时,PHP未定义索引数据
我试图使用Ajax POST请求更新mysql数据库到php文件,但收到以下错误:Javascript 当我尝试从Ajax发布时,PHP未定义索引数据,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我试图使用Ajax POST请求更新mysql数据库到php文件,但收到以下错误: Notice: Undefined index: data in C:\xampp\htdocs\php\php\post.php on line 2 Notice: Undefined index: data in C:\xampp\htdocs\php\php\post.php on line 2 {"d":true}{"d":true} 以下是我的Ajax代码: $('#addbut').click(f
Notice: Undefined index: data in C:\xampp\htdocs\php\php\post.php on line 2
Notice: Undefined index: data in C:\xampp\htdocs\php\php\post.php on line 2
{"d":true}{"d":true}
以下是我的Ajax代码:
$('#addbut').click(function()
{
console.log($("#firstteam").val());
console.log($("#score1").val());
console.log($("#score2").val());
console.log($("#secondteam").val());
var data = {
firstteam:$("#firstteam").val(),
secondteam:$("#secondteam").val(),
score1:$("#score1").val(),
score2:$("#score2").val()}
$("#firstteam").val('');
$("#secondteam").val('');
$("#score1").val('');
$("#score2").val('');
$.ajax({
type: "POST",
url: "php/post.php",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType:'json',
//if received a response from the server
success: function(response)
{
var res = response.d;
if (res == true)
{
$("#error").html("<div><b>Success!</b></div>"+response);
updateTable();
}
//display error message
else {
$("#error").html("<div><b>Information is Invalid!</b></div>"+response);
}
},
//If there was no resonse from the server
error: function(jqXHR, textStatus, errorThrown)
{
console.log("Something really bad happened " + textStatus);
$("#error").html(jqXHR.responseText);
}
});
});
$('#addbut')。单击(函数()
{
log($(“#firstteam”).val());
log($(“#score1”).val());
log($(“#score2”).val());
log($(“#secondteam”).val());
风险值数据={
firstteam:$(“#firstteam”).val(),
secondteam:$(“#secondteam”).val(),
分数1:$(“#分数1”).val(),
score2:$(“#score2”).val()}
$(“#第一队”).val(“”);
$(“#第二团队”).val(“”);
$(“#分数1”).val(“”);
$(“#分数2”).val(“”);
$.ajax({
类型:“POST”,
url:“php/post.php”,
数据:JSON.stringify(数据),
contentType:“应用程序/json;字符集=utf-8”,
数据类型:'json',
//如果收到来自服务器的响应
成功:功能(响应)
{
var res=响应d;
如果(res==true)
{
$(“#error”).html(“Success!”+response);
updateTable();
}
//显示错误消息
否则{
$(“#error”).html(“信息无效!”+响应);
}
},
//如果没有来自服务器的响应
错误:函数(jqXHR、textStatus、errorshown)
{
log(“发生了非常糟糕的事情”+textStatus);
$(“#error”).html(jqXHR.responseText);
}
});
});
下面是我的PHP代码:
<?php
$data = $_POST['data'] or $_REQUEST['data'];
$js = json_decode($data,true);
$t1 = $js['firstteam'];
$t2 = $js['secondteam'];
$s1 = $js['score1'];
$s2 = $js['score2'];
updateFunction($t1,$s1,$s2);
updateFunction($t2,$s2,$s1);
function updateFunction($name, $s1, $s2)
{
$conn = new mysqli("localhost:3306","root","","leagues");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($s1 > $s2)
$sql = "UPDATE league SET playedgames=playedgames + 1,wongames = wongames + 1,scoredgoal = '".$s1."', receivedgoal = '".$s2."', points = points + 3 WHERE teams='".$name."'";
else
if ($s1 == $s2)
$sql = "UPDATE league SET playedgames=playedgames + 1,tiegames = tiegames + 1,scoredgoal = '".$s1."', receivedgoal = '".$s2."', points = points + 1 WHERE teams='".$name."'";
else
if ($s1 < $s2)
$sql = "UPDATE league SET playedgames=playedgames + 1,lostgames = lostgames + 1,scoredgoal = '".$s1."', receivedgoal = '".$s2."' WHERE teams='".$name."'";
if ($conn->query($sql) === TRUE)
{
$response = json_encode(array('d' => true));
echo $response;
}
$conn->close();
}
?>
它不起作用,因为
$.ajax({
...
data: '{"key":"value"}',
...});
您只需将原始格式的字符串({“key”:“value”}
)放入请求体。
因此,没有名为data
的表单参数
要从正文检索此原始数据,请使用:
$data = file_get_contents('php://input');
OR
$data = stream_get_contents(STDIN);
而不是
$data = $_POST['data'] or $_REQUEST['data'];
它不起作用,因为
$.ajax({
...
data: '{"key":"value"}',
...});
您只需将原始格式的字符串({“key”:“value”}
)放入请求体。
因此,没有名为data
的表单参数
要从正文检索此原始数据,请使用:
$data = file_get_contents('php://input');
OR
$data = stream_get_contents(STDIN);
而不是
$data = $_POST['data'] or $_REQUEST['data'];
var\u dump($\u POST)可能重复代码>你有什么?数组(0){}这是我收到的var\u dump($\u POST)的可能副本你有什么?数组(0){}这是我收到的