dongsigan2044 2014-08-06 17:22
浏览 72
已采纳

用jquery调用ajax后用PHP清空$ _POST

I have a javascript function that collects data from some input fields and checkboxes.
I want to send that data to a PHP file and then return some information from a database.
But I have a problem getting the POST data in PHP so here I will only focus on that problem and won't deal with any database.

The data collected in the HTML form is returned in a Javascript object, I convert it to Json using JSON.stringify(data) and I get :

{"motscle":[""],"categories":[1,2,3],"prix":[{"min":0,"max":50},{"min":50,"max":100},{"min":100,"max":200},{"min":200,"max":500},{"min":500,"max":1000},{"min":1000,"max":2000}],"dimensions":{"longueur":"","largeur":"","hauteur":""}}

You can test it on http://jsonformatter.curiousconcept.com/ to see the expanded form and to see it's a valid JSON. So, the problem is not here I thnk.

Then I have an ajax call like that for testing purpose (var theJSON contains the above JSON string) :

 $.ajax({                                      
          url: 'post.php',                           
          data: theJSON ,   
          dataType: 'json', 
          error: function(){
               console.log("Error in ajax request");
          },
          success: function(data)          
          {
               console.log("Success of ajax request");
               console.log(data);
          }
     });

My testing PHP file post.php is like that :

<?php
     header('Content-Type: application/json; charset=utf-8');
     echo json_encode($_POST);
?>

The ajax call is OK as I get that in the js console :

Success of ajax request
[] 

However as you can see I have also a empty array []. I was expecting to get the $_POST content that PHP should have sent me.

I don't know where I'm wrong. Why don't I get the data in $_POST ?

  • 写回答

1条回答 默认 最新

  • dpyln64620 2014-08-06 17:26
    关注

    The default type of an ajax request is GET, either change the ajax

    $.ajax({                                      
          url: 'post.php',                        
          type : 'POST',
          data: theJSON ,   
          dataType: 'json', 
          error: function(){
               console.log("Error in ajax request");
          },
          success: function(data)          
          {
               console.log("Success of ajax request");
               console.log(data);
          }
     });
    

    or check GET

    echo json_encode($_GET);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64