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. UVALive 4254 Processor ——(二分+优先队列处理)

    题目是求最小化最大值,很显然是二分,但是二分以后怎么判断mid是否可行并不容易. 代码参考了网上一个博客的代码.巧妙之处在于一秒一秒的考虑,这样可以把处理速度mid直接转化成1秒内实际的量来解决(避免 ...

  2. HDU 1074 Doing Homework ——(状态压缩DP)

    考虑到n只有15,那么状压DP即可. 题目要求说输出字典序最小的答案的顺序,又考虑到题目给出的字符串本身字典序是递增的,那么枚举i的时候倒着来即可.因为在同样完成的情况下,后选字典序大的,小的字典序就 ...

  3. csp-s模拟99

    考前10天了... 昨天晚上真的不清醒,什么也码不对,心态爆炸. T1调了一个多小时没出来,T2因为少了一出q.pop()没A掉,T3随便写了几个sort竟然A了.十分懵逼. 最后20分钟想调T1,结 ...

  4. How To Display Variable Value In View?

    How To Display Variable Value In View? There are several ways. For example simply using @ like this: ...

  5. react hook的todolist

    感觉好长时间没写博客一样,app.js代码 import React from 'react'; import { useState } from 'react'; function App() { ...

  6. 全国数据json

    { "provinces": { "province": [ { "ssqid": "110000", "ss ...

  7. Heat map 绘图神奇

    https://study.163.com/provider/400000000398149/index.htm?share=2&shareId=400000000398149(博主视频教学主 ...

  8. Java同步数据结构之LinkedBlockingDeque

    前言 前面介绍完了队列Queue/BlockingQueue的实现类,接下来介绍双端队列Deque/BlockingDeque的实现类之一LinkedBlockingDeque,它是一种基于链表的可选 ...

  9. Flask request 属性详解

    Flask request 属性详解 一.关于request在Flask的官方文档中是这样介绍request的:对于 Web 应用,与客户端发送给服务器的数据交互至关重要.在 Flask 中由全局的 ...

  10. 阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_17-异常处理-可预知异常处理-异常处理测试

    ResultCode有很多的实现类 cmsCode有很多类型的错误异常 pgeService内当CmsPage这个对象为空的时候,异常类型就可以抛出CmsCode里面的错误代码 if(cmsPage1 ...