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 ...
随机推荐
- MySQL inodb cluster部署
innodb cluster是基于组复制来实现的. 搭建一套MySQL的高可用集群innodb. 实验环境: IP 主机名 系统 软件 192.168.91.46 master RHEL7.4 mys ...
- Eclipse生成EXE文件(可视化Login/读取文件)
Java Swing实现文件的简单读取 WindowBuilder的安装与使用 如何采用java设置一个登陆界面 package jp.services.slink2.batch.so2or; imp ...
- 从UDP的”连接性”说起–告知你不为人知的UDP
原文地址:http://bbs.utest.qq.com/?p=631 很早就计划写篇关于UDP的文章,尽管UDP协议远没TCP协议那么庞大.复杂,但是,要想将UDP描述清楚,用好UDP却要比TCP难 ...
- [java]取当前时间
/** * Get current date time * * @return */ private static String getCurrTime() { SimpleDateFormat sd ...
- 性能优化 | JVM性能调优篇——来自阿里P7的经验总结
VM 调优概述: 性能定义: 吞吐量 - 指不考虑 GC 引起的停顿时间或内存消耗,垃圾收集器能支撑应用达到的最高性能指标. 延迟 - 其度量标准是缩短由于垃圾啊收集引起的停顿时间或者完全消除因垃圾收 ...
- 网络编程三要素之IP
用来标示我们计算机在互联网上的唯一性 每个设备在网络中的唯一标识 每台网络终端在网络中都有一个独立的地址,我们在网络中传输数据就是使用这个地址. ipconfig:查看本机IP192.168.12.4 ...
- jemter 90%line的解释
假如: 有10个数: 1.2.3.4.5.6.7.8.9.10 按由大到小将其排列. 求它的第90%百分位,也就是第9个数刚好是9 ,那么他的90%Line 就是9 . 另一组数: 2.2.1. ...
- C# Winform中WebBrowser给网页中的input控件赋值/设置值
订阅WebBrowser的DocumentCompleted事件,在里面写入 private void browser_DocumentCompleted(object sender, WebBrow ...
- git_push报错
转自 https://blog.csdn.net/u010042585/article/details/79378726 将本地项目push到GitHub时遇到的问题 .$ git remote ad ...
- 通过命令行方式连接redis
1.首先安装redis客户端 yum install redis 2.连接 redis-cli -h host -p port -a password host:远程redis服务器host port ...