0
請參閱下面的代碼。爲什麼jQuery ajaxSend無法創建數據?
ajaxSend
我可以編輯或添加s.data
,但如果我在$ .post()中沒有提供任何其他數據,jQuery將發送無額外數據的請求。
有什麼建議嗎?
<?php if ($_SERVER['REQUEST_METHOD'] == 'POST') {
print_r($_POST);
} else { ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ajaxSend(function (e, xhr, s) {
s.data = s.data ? s.data+'&' : '';
s.data += 'my_value=1';
})
$.post('/test.php'); // empty $_POST
$.post('/test.php', {value: 0}); // $_POST with value and my_value
</script>
</head>
<body>
</body>
</html>
<?php } ?>
我看到它正確追加'my_value = 1'。你在看到什麼? –
@DanielT。 在第二個請求 Array( [value] => 0 [my_value] => 1 ) – ArtyGrand
這不是你想要的嗎? –