【问题标题】:Getting content-length but no content获取内容长度但没有内容
【发布时间】: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 将如何处理它?!)

标签: jquery json http lua cgi


【解决方案1】:
$.ajax({
    type:"POST",
    url:"ajaxtest.lp",
    dataType:'json',
    contentType:'json',
    data:{name:'lolbert',surname:'roflcopter'}
});

请尝试使用对象发送数据。

【讨论】:

  • 一切都保持不变,除了内容长度更短。
  • 尝试从 ajax 对象中删除 dataType 和 contentType 对象,所以它看起来像这样 $.ajax({ type:"POST", url:"ajaxtest.lp", data:{name:'lolbert ',姓氏:'roflcopter'} });如果这不起作用 - 那么链接有问题。请告诉我结果
【解决方案2】:

不是正确的 JSON 数据:http://www.json.org/ 尝试:

data: '{"name": "lolbert","surname": "roflcopter"}'

您可以使用在线工具(例如http://jsonlint.com/)检查正确的 JSON 格式。

【讨论】:

  • Content-length 现在是 31 而不是 37,Firebug 现在将传出的数据识别为 json,仍然存在缺少数据的问题。
  • 如果现在 Firebug 将传出数据识别为 json 可能问题出在服务器端... ajax test.lp 的内容是什么?顺便说一句,使用要发送的数据创建对象是个好主意。看看这个网址,或许能帮上忙:irayfuego.es/una-sencilla-llamada-ajax-con-jquery
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-07
  • 1970-01-01
  • 2012-05-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
相关资源
最近更新 更多