Ajax调用lua脚本导致iconv不能正常运行的问题

凤梨头头 2016-01-15 02:58:56
<html>
<head>
<script src="web/js/CryptoJS/rollups/sha1.js" type="text/javascript"></script>
<script src="web/js/jquery-1.4.2.min.js" type="text/javascript"></script>
</head>
<body>
<script>
var sha1=CryptoJS.SHA1("abc");
var sha1_Latin=sha1.toString(CryptoJS.enc.Latin1);
$.ajax({
type:'get',
url:"cgi-bin/test2.lua",
data:sha1_Latin,
datatype:"text",
success:function(data){
document.write(data);采用ajax时,输出lua返回iconv.new的返回值cd到屏幕,发现为空值
document.write("<br>");
}
})
</script>
</body>
</html>



#!/usr/bin/lua
print [[
Content-Type:text/plain\n\n

]]
require"sha1"
require"iconv"
function check_one(to,from,text)
local cd = iconv.new(to .. "//TRANSLIT", from)
print(cd)
local ostr,err =cd:iconv(text)

if err == iconv.ERROR_INCOMPLETE then
print("ERROR: Incomplete input.")
elseif err == iconv.ERROR_INVALID then
print("ERROR: Invalid input.")
elseif err == iconv.ERROR_NO_MEMORY then
print("ERROR: Failed to allocate memory.")
elseif err == iconv.ERROR_UNKNOWN then
print("ERROR: There was an unknown error.")
end
print(ostr)
return ostr
end

ch=os.getenv("QUERY_STRING")//有无这行对代码有影响
check_one("utf-8","iso-8859-1",sha1_binary("abc"))


ch=os.getenv("QUERY_STRING")是与ajax进行交互的,接收ajax发送的数据,假如注释掉这一行的时候,单独运行lua脚本,则check_one函数可以正常运行,check_one内的cd有返回值;但是当不注释掉这行,lua脚本和ajax交互时,即使lua端不使用接收到的数据ch,check_one依然不能正常工作,输出cd为空,即iconv.new无返回值
...全文
178 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jh_88 2017-03-04
  • 打赏
  • 举报
回复
你确认执行了吗sccuore?
凤梨头头 2016-01-17
  • 打赏
  • 举报
回复
[quote=引用 1 楼 showbo 的回复:] 您好,我按照您的试了一哈,提示的是success,可是还是没有iconv.new的返回值,很奇怪,一旦是用ajax调用服务器脚本的时候就会出现iconv.new无返回值,单独在服务器上运行lua脚本时候都可以正常的输出结果的。
Go 旅城通票 2016-01-16
  • 打赏
  • 举报
回复
你确认执行到success,而不是error回调?不要使用document.write,要不文档流关闭后调用这个会覆盖当前页面内容
   $.ajax({
        type: 'get',
        url: "cgi-bin/test2.lua",
        data: sha1_Latin,
        datatype: "text",
        success: function (data) {
            alert('success:' + data);///////
        },
        error: function (xhr) {/////////////
            alert('发生错误\n'+xhr.responseText)
        }
    })

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧