use*_*120 0 php codeigniter fiddler
这非常令人沮丧!我尝试将简单的ajax帖子发送到我的本地Web应用程序,并且该参数为空.为了调试它,我打开了小提琴手但是同样的结果正在发生!
我有这个控制器
class Site extends CI_Controller {
public function __construct() {
parent::__construct();
} //etc
Run Code Online (Sandbox Code Playgroud)
有了这个功能
public function hello() {
var_dump($_SERVER['REQUEST_METHOD']);
var_dump($_SERVER['REQUEST_URI']);
$hello = $this->input->post("hello");
echo "hello is $hello";
}
Run Code Online (Sandbox Code Playgroud)
现在我尝试在Fiddler中测试这个,我去Composer,选择一个新的Post请求并输入正文hello = wtsgppp.
这是fiddler中hello函数的结果:
string(4) "POST"
string(25) "/cmd/index.php/site/hello"
hello is
Run Code Online (Sandbox Code Playgroud)
你可以看到你好是空的.任何帮助都非常感谢!!
让事情变得更加烦人 - 一旦我将fiddler中的http请求更改为GET并在url字符串中添加hello(并将控制器更改为echo get而不是post),它就可以了!那么为什么要上班但是帖子没有?
PHP不会填充$_POST数组,除非它在标题中看到已知的内容类型.
在您的小提琴请求中,尝试添加以下HTTP标头:
Content-Type: application/x-www-form-urlencoded
这是因为PHP解码原始POST主体并将其注入$_POST变量(这是CI所看到的)的方式取决于请求发送到服务器的方式.
编辑:如果您没有在XHR/AJAX帖子中看到它,请确保您也设置了Content-Type标题.jQuery(以及大多数JS库)会自动为您执行此操作,但如果您不使用库,则必须自己执行此操作:
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");