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. JAVA RPC (九) netty服务端解析

    源码地址:https://gitee.com/a1234567891/koalas-rpc 企业生产级百亿日PV高可用可拓展的RPC框架.理论上并发数量接近服务器带宽,客户端采用thrift协议,服务 ...

  2. 微信浏览器H5开发常见的坑

    ios端兼容input光标高度 问题详情描述: input输入框光标,在安卓手机上显示没有问题,但是在苹果手机上 当点击输入的时候,光标的高度和父盒子的高度一样.例如下图,左图是正常所期待的输入框光标 ...

  3. 将elasticsearch设置为windows系统服务

    目前我都是在windows的环境下操作是Elasticsearch,并且喜欢使用命令行 启动时通过cmd直接在elasticsearch的bin目录下执行elasticsearch 这样直接启动的话集 ...

  4. 20175313 张黎仙《Java程序设计》第十周学习总结

    目录 一.教材学习内容总结 二.教材学习中的问题和解决过程 三.代码调试中的问题和解决过程 四.代码托管 五.心得体会 六.学习进度条 七.参考资料 一.教材学习内容总结 第十二章内容 主要内容 杂项 ...

  5. Linux设备驱动程序 之 kmalloc

    原型 kmalloc的原型如下: void *kmalloc(size_t size, gfp_t flags) 第一个参数是要分配的块的大小,第二个参数是分片标志: flags标志 最常用的标志是G ...

  6. count(1) 与 count(*) 比较

    1.  count(1) and count(*) 当表的数据量大些时,对表作分析之后,使用count(1)还要比使用count(*)用时多了! 从执行计划来看,count(1)和count(*)的效 ...

  7. ClipDrawable

    Clip可以译为剪的意思,我们可以把ClipDrawable理解为从位图上剪下一个部分; Android中的进度条就是使用ClipDrawable来实现的,他根据设置level的值来决定剪切 区域的大 ...

  8. kotlin嵌套类

    就是类中定义类 package loaderman.demo class Outer { var name: String = "name" inner class inner { ...

  9. 安装mysql问题解决

    [root@apollo init.d]# /etc/init.d/mysqld startmy_print_defaults: Can't read dir of '/etc/my.cnf.' (E ...

  10. [maven]maven插件 tomcat7-maven-plugin 的使用

    使用 tomcat7-maven-plugin,可以将tomcat内嵌到web项目中,直接运行webapp项目. 第一步.pom.xml的配置: <build> <plugins&g ...