PHP接收前端传值各种情况整理

服务端代码:


header('Access-Control-Allow-Origin:*');
var_dump($_POST);
exit;

情况

1) 传null


$.post('http://xxxxx.xx/index.php', {
"test": null
}, function(data, status) {
console.log(data);
});

结果:


array(1) {
["test"]=>
string(0) ""
}

2) 传''

代码:


$.post('http://xxxxx.xx/index.php', {
"test": ''
}, function(data, status) {
console.log(data);
});

结果:


array(1) {
["test"]=>
string(0) ""
}

3) 传'[]'


$.post('http://xxxxx.xx/index.php', {
"test": '[]'
}, function(data, status) {
console.log(data);
});

结果:


array(1) {
["test"]=>
string(2) "[]"
}

4) 传[]


$.post('http://xxxxx.xx/index.php', {
"test": []
}, function(data, status) {
console.log(data);
});

结果:


array(0) {
}

5) 传2个[]


$.post('http://xxxxx.xx/index.php', {
"test": [],
"test2": []
}, function(data, status) {
console.log(data);
});

结果:


array(0) {
}

6) 传{}


$.post('http://xxxxx.xx/index.php', {
"test": {}
}, function(data, status) {
console.log(data);
});

结果:


array(0) {
}

7) 传2个{}


$.post('http://xxxxx.xx/index.php', {
"test": {},
"test2": {}
}, function(data, status) {
console.log(data);
});

结果:


array(0) {
}

8) 传1个{}加1个非空对象


$.post('http://xxxxx.xx/index.php', {
"test": {},
"test2": {"a": 1}
}, function(data, status) {
console.log(data);
});

结果:


array(1) {
["test2"]=>
array(1) {
["a"]=>
string(1) "1"
}
}

9) 传[{}]


$.post('http://xxxxx.xx/index.php', {
"test": [{}]
}, function(data, status) {
console.log(data);
});

结果:


array(0) {
}

10) 传[[{}]]


$.post('http://xxxxx.xx/index.php', {
"test": [[{}]]
}, function(data, status) {
console.log(data);
});

结果:


array(0) {
}

11) 传'nil'


$.post('http://xxxxx.xx/index.php', {
"test": 'nil'
}, function(data, status) {
console.log(data);
});

结果:


array(1) {
["test"]=>
string(3) "nil"
}

12) 传0


$.post('http://xxxxx.xx/index.php', {
"test": 0
}, function(data, status) {
console.log(data);
});

结果:


array(1) {
["test"]=>
string(1) "0"
}

13) 传'null'


$.post('http://xxxxx.xx/index.php', {
"test": 'null'
}, function(data, status) {
console.log(data);
});

结果:


array(1) {
["test"]=>
string(4) "null"
}

用抓包工具发现

  1. http请求里面并不会发送"无效的"字段——[]和{},所以不是PHP丢弃了,而是没收到;
  2. 当传的值是js里的null,会转换成空字符串,http请求里面是test=,所以PHP接收到的test是个空字符串;
  3. http协议不能表示值是什么类型,所以PHP只能什么都当做string

总结:

  1. PHP对于接收到的每一个值,会转换成字符串变量
  2. PHP对于接收到的,之所有会接收不到是因为被一系列规则过滤掉了

以上结论是在jQ和PHP7之下验证的,其他环境不一定保证正确,之后可以试验使用CURL发送数据试试。

TODO:

  • [ ] 用CURL发送POST测试

原文链接:https://my.oschina.net/wiiilll/blog/3002507

PHP接收前端传值各种情况整理的更多相关文章

  1. SpringMVC接收前端传值有哪些方式?

    有很多种,比如: 1.通过@RequestParam注解接收请求参数: 2.通过Bean封装,接收多个请求参数 3.通过@ModelAttribute绑定接收前端表单数据 4.通过@PathVaria ...

  2. SpringBoot 后端接收前端传值的方法

    1.通过HttpServletRequest接收,适用于GET 和 POST请求方式       通过HttpServletRequest对象获取请求参数 @RestController @Reque ...

  3. 关于mui前端传值,springboot后台接收值的问题

    最近做app,使用mui的ajax给后台传参,后台一直接收不到值,表示很蛋疼.这里通过网上搜索加上个人实践,总结归纳了三种前端传值和后台接收的方式. 第一种: 前端: data: JSON.strin ...

  4. Java如何接收前端传来的多层嵌套的复杂json串

    想看问题直接解决方式,直接拉到博文底部. Spring的controller在接收前端传参的时候如果参数使用@RequestBody标注的时候 @RequestBody 则会把前端参数转为JSON的形 ...

  5. web开发前端面试知识点目录整理

    web开发前端面试知识点目录整理 基本功考察 关于Html 1. html语义化标签的理解; 结构化的理解; 能否写出简洁的html结构; SEO优化 2. h5中新增的属性; 如自定义属性data, ...

  6. SpringBoot接收前端参数的三种方法

    都是以前的笔记了,有时间就整理出来了,SpringBoot接收前端参数的三种方法,首先第一种代码: @RestController public class ControllerTest { //访问 ...

  7. EF5+MVC4系列(7) 后台SelectListItem传值给前台显示Select下拉框;后台Action接收浏览器传值的4种方式; 后台Action向前台View视图传递数据的四种方式(ViewDate,TempDate,ViewBag,Model (实际是ViewDate.Model传值))

    一:后台使用SelectListItem 传值给前台显示Select下拉框 我们先来看数据库的订单表,里面有3条订单,他们的用户id对应了 UserInfo用户表的数据,现在我们要做的是添加一个Ord ...

  8. THINKPHP_(7)_THINKPHP6的controller模型接收前端页面通过ajax返回的数据,会因为一个div而失败

    这个随笔比较短. 同样的前端页面代码,修改了一下,后端模型接收不到数据. 利用beyond compare软件比对两个前端文件, 发现多了一个</div>标签. 多了一个</div& ...

  9. .net 前端传值和后端接收的几种方式

    第一种:GET传参(常用): get传参方式就是链接?后写上参数和数据用&拼接. 第二种:POST传参(常用): 这种传参方式可以GET POST同时传,在链接上加参数后台用get方式接收,P ...

随机推荐

  1. 【面试题总结】1、统计字符串中某个单词出现的次数(1-C++实现)

    [解决方法一]C++ map解决 一.map中的find函数: 用于查找map中是否包含某个关键字条目,传入的参数是要查找的key,最后返回一个迭代器,如果没有找到,则返回的迭代器等于end()返回的 ...

  2. How do negative margins in CSS work and why is (margin-top:-5 != margin-bottom:5)?

    How do negative margins in CSS work and why is (margin-top:-5 != margin-bottom:5)? 解答   Negative mar ...

  3. AnimationDrawable

    ①先定义一个AnimationDrawable的xml资源文件: <?xml version="1.0" encoding="utf-8"?> &l ...

  4. HTTP的响应协议

    响应行介绍,响应状态码 1XX: 客户端请求服务器,但是请求未完成,服务器什么事也没干 2XX: 表示响应成功,代表性的状态码就是200 3XX: 请求重定向,代表性的状态码302 4XX: 客户端发 ...

  5. js es6遍历对象的6种方法(应用中推荐前三种)

        javaScript遍历对象总结 1.for … in 循环遍历对象自身的和继承的可枚举属性(循环遍历对象自身的和继承的可枚举属性(不含Symbol属性).). 2.使用Object.keys ...

  6. Eclipse项目修改编译jdk版本(Failed to read candidate component class: file 处理)

    转: Failed to read candidate component class: file 处理 2018年03月09日 07:15:54 爱萨萨 阅读数 10041   出错现象: org. ...

  7. REPLACE 语法

    转自:https://www.cnblogs.com/jiangzhengjun/p/4292994.html#_Toc411766043 REPLACE REPLACE [{FIRST OCCURR ...

  8. java.lang.IllegalStateException: No primary or default constructor found for class java.time.LocalDate

    转载自:https://blog.csdn.net/Coder_Arley/article/details/81910705 springboot中报错如下: springmvc也可以使用类似处理方法 ...

  9. [C++]多源最短路径(带权有向图):【Floyd算法(动态规划法)】 VS n*Dijkstra算法(贪心算法)

    1 Floyd算法 1.1 解决问题/提出背景 多源最短路径(带权有向图中,求每一对顶点之间的最短路径) 方案一:弗洛伊德(Floyd算法)算法 算法思想:动态规划法 时间复杂度:O(n^3) 形式上 ...

  10. haproxy配置文件实例

    [root@kube-node1 ~]# cat /etc/haproxy/haproxy.cfg global log /dev/log local0 log /dev/log local1 not ...