[英]AJAX won't send POST data
I'm currently working on a project which requires some AJAX, but for some reason my current code wont send the POST data. 我目前正在一个需要一些AJAX的项目中,但是由于某种原因,我当前的代码无法发送POST数据。 here is my AJAX:
这是我的AJAX:
<script>
$('#greenon').click(function(){
$.ajax({
type: 'POST',
url: 'command.php',
data : { "green" : "on" },
success: function(data, textStatus, jqXHR) {
alert("it worked!");
},
error: function(jqXHR, textStatus, errorThrown) {
alert("didn't work!");
}
});
return false;
});
</script>
And it's not even sending the 'didnt work' alert. 而且它甚至没有发送“辛勤工作”警报。 I am using this in command.php (it would write the file if command.php got accessed,
我在command.php中使用了它(如果访问command.php它将写入文件,
<?php
$content = "bleh";
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/file.txt","wb");
fwrite($fp,$content);
fclose($fp);
?>
Why is it not sending POST data? 为什么不发送POST数据? thanks!
谢谢!
oh and here is my button HTML: 哦,这是我的按钮HTML:
<a class="btn btn-success" role="button" id="greenon">Turn On</a>
It seems that your JS code is before your's button HTML code, so the binding doesn't work. 您的JS代码似乎在按钮的HTML代码之前,因此绑定无效。 Put your JS after
<a>...</a>
, or frame it with $(document).ready()
callback: 把你的JS 后
<a>...</a>
,或者用镜框$(document).ready()
回调:
<script>
$(document).ready(function() {
$('#greenon').click(function(){
$.ajax({
type: 'POST',
url: 'command.php',
data : { "green" : "on" },
success: function(data, textStatus, jqXHR) {
alert("it worked!");
},
error: function(jqXHR, textStatus, errorThrown) {
alert("didn't work!");
}
});
return false;
});
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.