我有兩個簡單的PHP頁面:jQuery的AJAX POST請求錯誤值傳遞
notification.php
<html>
<head><title></title>
<meta charset="UTF-8">
<script src="https://cdn.firebase.com/js/client/2.4.2/firebase.js"></script>
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script src="https://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
</head >
<body>
<script language="JavaScript" >
function gotData(data) {
var toSave = "token=22"; //just a try not a real token
toSave +="&";
var allPropertyNames = Object.keys(data);
var SIZEITEM = allPropertyNames.length;
for (var j=0; j<SIZEITEM; j++) {
var name = allPropertyNames[j];
var value = data[name];
toSave +=name+"="+value;
if(j < SIZEITEM-1)
toSave +="&";
}
console.log(toSave);
$.ajax({
url : "notification_okvalue.php",
type: "POST",
dataType : 'text',
data : {toSave:toSave},
success : function(sended) {
console.log("sucessfull sending:")
console.log(sended);
},
error : function() {
console.log('failed');
}
});
}
</script>
<script src="https://MYFIREBASE.firebaseio.com/chats/-KF4foKsJxIFbLEVxdNC/group.json?callback=gotData"></script>
</body>
</html>
而且notification_okvalue.php
<html>
<head>
<title></title>
</head>
<body>
<script language="JavaScript">
var ref = new Firebase("https://MYFIREBASE.firebaseio.com/");
ref.child("prova").set(
{
all_ok:"<?php echo $_POST["token"];?>";
});
</script>
</body>
</html>
控制檯給我發送成功串 和console.log(toSave)
給我正確的字符串
令牌= 22 & -KF4foL4E4E2V4oPU-CI = -KF4fi_gWTE4MQ5Bqskw & -KF4foL7g09NGKnQbouU = -KF4fJpvZC6LgbltEesZ
但是,當調用傳遞整個代碼POST確實console.log(sended)
還給我整個notification_okvalue.php代碼
當然,我收到錯誤的notification_okvalue.php是$_POST["token"] is not setted
編輯 對於更具體一些,我實際上並不需要notification_okvalue.php
的響應,但我需要的是,所有傳遞的$ _POST值必須存儲在Firebase中。所以我需要的是Firebase寫道。
刪除數據類型:「文字」或變「文本」,以JSON或任何你正在返回。 – Webice
我正在返回上面的'token = 22&-KF4foL4E4E2V4oPU-cI = -KF4fi_gWTE4MQ5Bqskw&-KF4foL7g09NGKnQbouU = -KF4fJpvZC6LgbltEesZ'的字符串,我嘗試刪除dataType,並且也切換到json。當我刪除它,結果是一樣的,與json的調用失敗 – Alfox
我認爲你的toSave被格式化爲查詢字符串和[這是錯誤的](http://stackoverflow.com/questions/5876809/do-http-post-方法-發送數據作爲一種-查詢字符串)。創建一個JSON對象來代替:'{'token':22,// etc ...}' – KWeiss