dongweng6241 2016-04-13 12:20
浏览 39
已采纳

jQuery AJAX POST请求错误传递值

I have two simple php pages:

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>

And 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>

The console give me the successful sending string and the console.log(toSave) give me the right string

token=22&-KF4foL4E4E2V4oPU-cI=-KF4fi_gWTE4MQ5Bqskw&-KF4foL7g09NGKnQbouU=-KF4fJpvZC6LgbltEesZ

But when the call pass the entire code as POST indeed the console.log(sended) return me the entire notification_okvalue.php code

And of course I receive the error in notification_okvalue.php that $_POST["token"] is not setted

EDIT For be more specific, I don't actually need the response of the notification_okvalue.php but what I need is that, all the $_POST value passed must be stored in Firebase. So what I need is the Firebase writes.

  • 写回答

3条回答 默认 最新

  • duanbei3704 2016-04-13 13:31
    关注

    I think structuring your POST data as a querystring is wrong here. Compare this question.

    You may have more success if you just use your data object:

    var postData = data;
    postData.token = 22;
    

    And then in your $.post:

    data: postData
    

    That way the token should arrive correctly. You probably will still get the whole of the notification_okvalue.php as a response, though.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突