【发布时间】:2016-03-29 07:23:32
【问题描述】:
我有一个纯 javascript 代码,我需要发送一个 JSON(可能很大,因此超过了 GET 限制....),所以我想使用 AJAX 进行 POST 请求。
我的代码是...
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Test POST via AJAX</title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script>
var waypoints = [{
"options": {
"allowUTurn": false
},
"latLng": {
"lat": 44.911434,
"lng": 7.671602
},
"name": "Corso Vinovo, Carignano, TO, Piemonte, Italia",
"_initHooksCalled": true
}, {
"options": {
"allowUTurn": false
},
"latLng": {
"lat": 44.910167,
"lng": 7.676274
},
"name": "Il Tempio del Pane, Corso Cesare Battisti, Carignano, TO, Piemonte, Italia",
"_initHooksCalled": true
}, {
"options": {
"allowUTurn": false
},
"latLng": {
"lat": 44.908034,
"lng": 7.675075
},
"name": "Banca Intesa, Via Ferdinando Salotto, Carignano, TO, Piemonte, Italia",
"_initHooksCalled": true
}];
alert ('Waypoints --> ' + JSON.stringify(waypoints));
$.ajax({
type: "POST",
url: "NewPage.php",
contentType: "application/json",
data: JSON.stringify(waypoints),
success: function()
{
alert("OK ...");
},
error: function()
{
alert("KO ...")
}
})
alert('After post request ...');
</script>
</body>
</html>
这里是 NewPage.php 的代码
<?php
echo 'Waypoints ?????';
print_r($_POST)
?>
执行代码我可以看到我的 AJAX 代码提醒我“OK”,但我看不到关于我的 NewPage.php 的任何内容
我对这些东西很陌生,很抱歉平庸....
任何建议,例如,jsfiddle,替代?
提前非常感谢!
切萨雷
【问题讨论】:
-
你想从你的 NewPage.php 中看到什么?
-
你的成功回调中没有任何返回变量。它应该从服务器端获取结果,在打印后放置骰子或退出。
-
现在我想看看(echo ...),从第一个html页面发送的JSON,我将把我的业务代码放在...之后。谢谢
-
将
data添加到您的success中,例如:-success: function(data)并检查data中返回的内容。 -
$data = $_POST['waypoints']; echo json_encode($data);在 php 中$.ajax({ type: "POST", url: "NewPage.php", dataType: "json", data: { waypoints: JSON.stringify(waypoints) }, success: function(data) { alert(data); }, error: function() { alert("KO ...") } })在 ajax 中
标签: javascript jquery json ajax post