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学习日记——基础篇(一)常识

    JAVA简介 Java的标准 Java是一种语言,一个平台包含JavaSE.JavaEE.JavaME三个版本 JavaSE标准版(属于Java的基础部分,可以开发C/S构架的桌面应用程序) Java ...

  2. 重读APUE(4)-fcntl和ioctl的区别

    fcntl(File Control)-文件控制 ioctl(In/Out Control)-I/O控制 1. fcntl作用于文件,提供对文件的基础控制:ioctl作用于文件和设备对象,一般用来向设 ...

  3. httpd Apache服务

    TCP/IP协议 跨Internet的主机间通讯 在建立通信连接的每一端,进程间的传输要有两个标志: IP地址和端口号,合称为套接字地址 socket address 客户机套接字地址定义了一个唯一的 ...

  4. 第11组 Beta冲刺(5/5)

    第11组 Beta冲刺(5/5)   队名 不知道叫什么团队 组长博客 https://www.cnblogs.com/xxylac/p/12031050.html 作业博客 https://edu. ...

  5. Python JSON dump ,load,dumps,loads

    JSON是一种轻量级的数据交换格式. json.dump() 将Python数据格式序列化为json数据格式(字符串)并储存在json文件之中. json.load()将Jons数据(字符串)反序列化 ...

  6. 4 个独特的 Linux 终端模拟器(转)

    4 个独特的 Linux 终端模拟器 译自:https://www.linux.com/blog/learn/2018/12/4-unique-terminals-linux作者: Jack Wall ...

  7. 4.git的基本命令

    版本库 index 暂存区,HEAD 将来1.0,2.0的指向  多次add,一次commit 每次commit一次,head就指向了最新的版本.head是回退版本的时候会用到 一般有开发的分支,ma ...

  8. dlopen, dlsym今天才刚知道干什么用的,羞死人了

    dlopen, dlsym今天才刚知道干什么用的,羞死人了

  9. JKD1.8新特性

    1.Optional类 Optional是jdk1.8引入的类型,Optional是一个容器对象,它包括了我们需要的对象,使用isPresent方法判断所包 含对象是否为空,isPresent方法返回 ...

  10. Spark中foreachRDD的正确使用

    常出现的使用误区: 误区一:在driver上创建连接对象(比如网络连接或数据库连接)    如果在driver上创建连接对象,然后在RDD的算子函数内使用连接对象,那么就意味着需要将连接对象序列化后从 ...