好的,这是我的问题。Ajax的数据参数没有送到服务器
我有一个HTML页面,其中已初始化一个JavaScript变量。
<html>
<script>
MyVaribale = "Random Data";
</script>
<!-- Then I include an external js file to handle the processes in this html file -->
<script type="text/javascript" language="javascript" src="/scripts/some_random_script.js"></script>
</html>
现在,在该脚本中。我用MyVaribale
varible在Ajax请求有一个,像这样:
$(document).ready(function() {
$.ajax(
url : '/some/random/url',
data : { MyVariable : MyVaribale }
etc ...
);
});
所以,在页面加载,即Ajax代码,立即执行。
在上面指定的url中,我检查了MyVaribale
的存在,然后标记一个错误,如果它不存在,则为必需值。
后端这样的代码(在Perl):
my $MyVariable = trim_param('MyVariable'); # trim_param() is a function that gets the passed data from ajax.
if ($MyVariable) { # Test if it exists
# Display something
}
else {
# Flag an error, which is my problem
}
现在我相信,在HTML页面,该变量总是被填充(是100%确定)。但我总是得到标志错误,该值不存在于我的后端代码(上面的URL)中。
这样的问题,
是否阿贾克斯一些问题与document.ready
,也许它执行变量已完成分配一个值之前?任何想法为什么发生这种情况因为有时我的Ajax请求是成功的,有时不是
感谢
你可以发布你的后端代码吗? – 2015-03-25 07:18:22
@HastaPasta编辑我的文章 – user2881063 2015-03-25 07:29:10
在'$(document).ready(function(){'如果你添加'console.log(MyVaribale)'你看到控制台中变量的内容吗? – 2015-03-25 07:31:51