【发布时间】:2016-04-23 17:04:34
【问题描述】:
我正在发送一个带有数据的 POST 请求,但我没有收到任何数据。
调用请求:
$.ajax({
type:"POST",
url:"ajaxtest.lp",
dataType:'json',
contentType:'json',
data:"{name:'lolbert',surname:'roflcopter'}"
})
发生了什么:
标题:
Connection
close
Content-Type
text/html
Date
Thu, 01 Jan 1970 07:24:40 GMT
Server
Core4Web
Quelltext anzeigen
Accept
application/json, text/javascript, */*; q=0.01
Accept-Encoding
gzip, deflate
Accept-Language
de,en-US;q=0.7,en;q=0.3
Content-Length
31
Content-Type
json; charset=UTF-8
Host
192.168.207.117
Referer
http://192.168.207.117/ajaxtest.lp
User-Agent
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0
X-Requested-With
XMLHttpRequest
POST(通过 Firebug):
name=lolbert&surname=roflcopter
但是我在服务器端收到的内容有点奇怪。 内容长度正确(37 个字符),但缺少内容本身。
**** 请求
script_file 请求 script_file ajaxtest.lp
client_content_type 请求 client_content_type json;字符集=UTF-8
client_port 请求 client_port 64626
方法请求方法post
client_address 请求 client_address 192.168.203.59
client_content_length 请求 client_content_length 37
骨灰盒请求骨灰盒 /ajaxtest.lp
script_vpath 请求 script_vpath /ajaxtest.lp
script_path 请求 script_path /usr/lib/htdocs/ajaxtest.lp
script_pdir 请求 script_pdir /usr/lib/htdocs/
script_vdir 请求 script_vdir /
**** 标题
Content-Type 头 Content-Type json;字符集=UTF-8
Accept-Encoding 标头 Accept-Encoding gzip,放气
X-Requested-With 标头 X-Requested-With XMLHttpRequest
Accept-Language 标头 Accept-Language de,en-US;q=0.7,en;q=0.3
连接头连接保持活动
Pragma header Pragma no-cache
Content-Length header Content-Length 37
主机头主机 192.168.207.117
User-Agent 标头 User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0
Accept header Accept application/json, text/javascript, /; q=0.01
Referer 头 Referer http://192.168.207.117/ajaxtest.lp
Cache-Control 标头 Cache-Control no-cache
**** 文件
script_path 文件 script_path /usr/lib/htdocs/ajaxtest.lua
script_pdir 文件 script_pdir /usr/lib/htdocs/
瓮文件瓮 ajaxtest.lua
script_vpath 文件 script_vpath /ajaxtest.lua
嵌套层文件嵌套层 1
script_file 文件 script_file ajaxtest.lua
script_vdir 文件 script_vdir /
【问题讨论】:
-
您应该将对象作为数据传递给
$.post方法,而不是字符串:data:{"name":"lolbert","surname":"roflcopter"}。并将其设置为有效的 JSON 表示法,因为您正在设置contentType:'json',(甚至不确定 jQuery 将如何处理它?!)