clearsiver cgi获取到的post数据为空问题

来源:互联网 发布:just do it 乔丹 编辑:程序博客网 时间:2024/05/18 00:12

问题描述:

在封装cgi库支持fastcgi从而在Nginx下通过spawn-fcgi方式运行时,发现通过jQuery ajax方式POST提交的数据在cgi侧获取为空。

 

1.先确认数据是否确实提交到服务器

工具:Fiddler 

确实发送了

 

2.是否是Nginx转发出了问题

打开Nginx日志,添加$request_body,重启Nginx

确实转发了

 

线索:通过form表单 submit方式post提交,可以获取到数据

抓包看看form表单提交和ajax方式提交有啥区别

 

 多出项不同项表单提交X-Requested-With: XMLHttpRequestContent-Type: application/x-www-form-urlencoded$ajax方式Upgrade-Insecure-Requests: 1Content-Type: application/x-www-form-urlencoded; charset=UTF-8

 

3.通过截包并篡改头部确认关键点

Fiddler -> Rules -> Automatic Breakpoints -> Before Requests 或者F11

发现,当将ajax提交的Content-Type中的“; charset=UTF-8”去掉后,cgi可以获取到post数据。

 

4.猜测cgi使用的clearsilver框架对post方式解析数据存在问题

clearsilver源码:https://github.com/jeske/Clearsilver/blob/master/cgi/cgi.c

改为if (type && !strncmp(type, "application/x-www-form-urlencoded", 33))即可。

 

$ajax方式post数据时,Content-Type为何会多出后面的部分?

Jquery官网: http://api.jquery.com/jQuery.ajax/

 

规避方式2:在js提交请求时显示指定contentType

    $.ajax({        type: "POST",        url: url,        contentType:"application/x-www-form-urlencoded",        data: postData,        dataType:"json",        success: function(result){            if (typeof(result)!= "undefined" && 0 == result.code)            {                  //console.log('suc');            }        }    });

 

clearsilver官网:http://www.clearsilver.net/

0 0
原创粉丝点击