PHP接收前端传值各种情况整理
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"
}
用抓包工具发现
- http请求里面并不会发送
"无效的"
字段——[]和{},所以不是PHP丢弃了,而是没收到; - 当传的值是js里的
null
,会转换成空字符串,http请求里面是test=
,所以PHP接收到的test是个空字符串; - http协议不能表示值是什么类型,所以PHP只能什么都当做string
总结:
- PHP对于接收到的每一个值,会转换成字符串变量
- PHP对于接收到的,之所有会接收不到是因为被一系列规则过滤掉了
以上结论是在jQ和PHP7之下验证的,其他环境不一定保证正确,之后可以试验使用CURL发送数据试试。
TODO:
- [ ] 用CURL发送POST测试
原文链接:https://my.oschina.net/wiiilll/blog/3002507
PHP接收前端传值各种情况整理的更多相关文章
- SpringMVC接收前端传值有哪些方式?
有很多种,比如: 1.通过@RequestParam注解接收请求参数: 2.通过Bean封装,接收多个请求参数 3.通过@ModelAttribute绑定接收前端表单数据 4.通过@PathVaria ...
- SpringBoot 后端接收前端传值的方法
1.通过HttpServletRequest接收,适用于GET 和 POST请求方式 通过HttpServletRequest对象获取请求参数 @RestController @Reque ...
- 关于mui前端传值,springboot后台接收值的问题
最近做app,使用mui的ajax给后台传参,后台一直接收不到值,表示很蛋疼.这里通过网上搜索加上个人实践,总结归纳了三种前端传值和后台接收的方式. 第一种: 前端: data: JSON.strin ...
- Java如何接收前端传来的多层嵌套的复杂json串
想看问题直接解决方式,直接拉到博文底部. Spring的controller在接收前端传参的时候如果参数使用@RequestBody标注的时候 @RequestBody 则会把前端参数转为JSON的形 ...
- web开发前端面试知识点目录整理
web开发前端面试知识点目录整理 基本功考察 关于Html 1. html语义化标签的理解; 结构化的理解; 能否写出简洁的html结构; SEO优化 2. h5中新增的属性; 如自定义属性data, ...
- SpringBoot接收前端参数的三种方法
都是以前的笔记了,有时间就整理出来了,SpringBoot接收前端参数的三种方法,首先第一种代码: @RestController public class ControllerTest { //访问 ...
- EF5+MVC4系列(7) 后台SelectListItem传值给前台显示Select下拉框;后台Action接收浏览器传值的4种方式; 后台Action向前台View视图传递数据的四种方式(ViewDate,TempDate,ViewBag,Model (实际是ViewDate.Model传值))
一:后台使用SelectListItem 传值给前台显示Select下拉框 我们先来看数据库的订单表,里面有3条订单,他们的用户id对应了 UserInfo用户表的数据,现在我们要做的是添加一个Ord ...
- THINKPHP_(7)_THINKPHP6的controller模型接收前端页面通过ajax返回的数据,会因为一个div而失败
这个随笔比较短. 同样的前端页面代码,修改了一下,后端模型接收不到数据. 利用beyond compare软件比对两个前端文件, 发现多了一个</div>标签. 多了一个</div& ...
- .net 前端传值和后端接收的几种方式
第一种:GET传参(常用): get传参方式就是链接?后写上参数和数据用&拼接. 第二种:POST传参(常用): 这种传参方式可以GET POST同时传,在链接上加参数后台用get方式接收,P ...
随机推荐
- webpack - 优化阻塞渲染的css
随着浏览器的日新月异,网页的性能和速度越来越好,并且对于用户体验来说也越来越重要. 现在有很多优化页面的办法,比如:静态资源的合并和压缩,code splitting,DNS预读取等等. 本文介绍的是 ...
- springboot工程打成war包
1.将pom.xml中默认的jar修改为war. <packaging>war</packaging> 2.排除SpringBoot内置的Tomcat容器. <depen ...
- Oracle JDBC 连接池
1.简介 数据库连接池负责分配.管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而不是再重新建立一个:释放空闲时间超过最大空闲时间的数据库连接来避免因为没有释放数据库连接而引起的数据 ...
- Server2012R2实现活动目录(Active Directory)双域控制器互为冗余
在活动目录中部署两台主控域控制器,两台域控制器互为冗余. Server 2012 R2新建活动目录和DC refer to: https://www.cnblogs.com/jfzhu/p/40061 ...
- DNS -- 快速清除DNS缓存
MAC: sudo dscacheutil -flushcache Linux: dnsmasq的是一个轻量级的DNS.TFTP和DHCP服务器.它的目的是给局域网提供配对的DNS和DHCP服务. d ...
- Mybatis generator配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...
- 如何从社区的patchwork下载补丁并应用到当前内核源码?
1. 下载 wget http://patchwork.ozlabs.org/series/111111/mbox 2. 打补丁 git am mbox
- SQL-W3School-高级:SQL 撤销索引、表以及数据库
ylbtech-SQL-W3School-高级:SQL 撤销索引.表以及数据库 1.返回顶部 1. 通过使用 DROP 语句,可以轻松地删除索引.表和数据库. SQL DROP INDEX 语句 我们 ...
- SQL-W3School-高级:SQL FOREIGN KEY 约束
ylbtech-SQL-W3School-高级:SQL FOREIGN KEY 约束 1.返回顶部 1. SQL FOREIGN KEY 约束 一个表中的 FOREIGN KEY 指向另一个表中的 P ...
- apt如何列出所有已经安装的软件包
apt如何列出所有已经安装的软件包 转 https://www.helplib.com/ubuntu/article_155294 问题: 我想将所有已安装软件包的列表输出到文本文件中,以便我可以查看 ...