PHP接收前端传值各种情况整理 原 荐

栏目: ASP.NET · 发布时间: 7年前

PHP接收前端传值各种情况整理

  SSSWIIILLL 发布于 今天 08:23

字数 535

阅读 13

收藏 0

curl PHP

程序员们,在北上广你还能买房吗? >>> 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"
}

用抓包 工具 发现

  1. http请求里面并不会发送 "无效的" 字段——[]和{},所以不是 PHP 丢弃了,而是没收到;
  2. 当传的值是js里的 null ,会转换成空字符串,http请求里面是 test= ,所以PHP接收到的test是个空字符串;
  3. http协议不能表示值是什么类型,所以PHP只能什么都当做string

总结:

  1. PHP对于接收到的每一个值,会转换成字符串变量
  2. PHP对于接收到的,之所有会接收不到是因为被一系列规则过滤掉了

以上结论是在jQ和PHP7之下验证的,其他环境不一定保证正确,之后可以试验使用CURL发送数据试试。

TODO:

  • [ ] 用CURL发送POST测试

© 著作权归作者所有

共有人打赏支持

上一篇: LINUX下进程打开的文件怎么和底层磁盘关联的?

下一篇: Composer简易教程

PHP接收前端传值各种情况整理 原 荐

SSSWIIILLL

粉丝 0

博文 5

码字总数 2552

作品 0

浦东

提问

相关文章 最新文章

网站流量统计,又一个造轮子的事....,求方案

前端页面是php的,页底加了js异步调用统计接口(java写的)当然传了一堆参数,频道,url等等...... 至于为啥这么做,此处省略xxxx字,我也知道这是个造轮子的事..... 目前情况是接收请求后加入队列,...

老鼠盘根

2014/03/18

193

1

前端传参后台接收类型方式

有人说 : 前端传参数给后端,不管传多少个参数,都使用参数对象来接收 比如: 前端 x=12 后端就建一个A类 定义一个private int x;属性来接收 如果别的接口也需要这个参数,且还需要额外的参数,那...

_大侠__

2018/09/12

542

5

Vue数据传递--教你特殊的实现技巧

最近碰到了比较多的关于vue的eventBus的问题,之前定技术选型的时候也被问到了,vuex和eventBus的使用范围。所以简单的写一下。同时有一种特殊的实现方案。有这么几种数据传递方式,vuex、p...

前端攻城老湿

2018/12/13

0

0

ajax和jsonp没有半点关系,跨域问题

jsonp确实和ajax没有半毛钱关系,只算是一种机制跨域数据获取方案或者协议。 只是说很多库(比如jquery)里面对jsonp和ajax做了一样的封装,看起来就好像jsonp是ajax的一部分一样,这好像会造...

SubinY

2016/12/26

32

0

SpringMVC后台接收list类型的数据的实现方式

一、背景 最近在做一些东西的时候,遇到一个需要Springmvc后台接收list类型数据的需求,几经辗转才完美解决了这个问题,今天记下来方便以后使用,也分享给需要的小伙伴们~ 二、实现方式 ...

hafiz.zhang

2016/05/16

0

0

PHP接收前端传值各种情况整理 原 荐

没有更多内容

加载失败,请刷新页面

加载更多
 tomcat启动问题

tomcat启动问题: 解决方法:tomcat bin目录下 新建setenv.sh文件 文件上加入 export CATALINA_OPTS='-Djava.awt.headless=true' 该方法对tomcat8以上有用...

west_coast

10分钟前

1

0

PHP接收前端传值各种情况整理 原 荐
《从0开始学架构》学习笔记索引

一、架构设计相关概念及复杂度来源 二、架构设计三原则 三、架构设计流程 ... 完整课程(微信扫码):

whoru

23分钟前

1

0

PHP接收前端传值各种情况整理 原 荐
新的尝试!ComponentOne WinForm 和 .NET Core 3.0

在微软 Build 2018 开发者大会上,.NET 团队公布了 .NET Core 的下一个主要版本 .NET Core 3.0 的规划蓝图:.NET Core 3将开始支持Windows桌面应用程序,包括Windows Form、Windows Present...

葡萄城技术团队

25分钟前

0

0

PHP接收前端传值各种情况整理 原 荐
Try .NET & Github Gist

Try .NET Try .NET 是微软最近推出的在线 C# 运行环境,不用安装 Visual Studio 等,就可以直接上手写 C# 代码. 可以先进来写两行代码看看 https://try.dot.net/ 对于初学者/代码分享而言是一个...

taadis

27分钟前

21

0

PHP接收前端传值各种情况整理 原 荐
PyCharm入门教程——掌握PyCharm键盘快捷键

PyCharm最新版本下载 JetBrains PyCharm是一种Python IDE,其带有一整套可以帮助用户在使用 Python 语言开发时提高其效率的工具。此外,该IDE提供了一些高级功能,以用于Django框架下的专业Web...

电池盒

32分钟前

0

0

PHP接收前端传值各种情况整理 原 荐

没有更多内容

加载失败,请刷新页面

加载更多

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Paradigms of Artificial Intelligence Programming

Paradigms of Artificial Intelligence Programming

Peter Norvig / Morgan Kaufmann / 1991-10-01 / USD 77.95

Paradigms of AI Programming is the first text to teach advanced Common Lisp techniques in the context of building major AI systems. By reconstructing authentic, complex AI programs using state-of-the-......一起来看看 《Paradigms of Artificial Intelligence Programming》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具