我有一个使用AJAX get方法调用PHP脚本的Javascript。如果我在外部运行PHP脚本,它可以正常工作并创建文件connections.txt。但是使用JS它无法正常工作
$(document).on("click", "#target2", function(){
var arr = ["one","two","three"];
$.ajax({
type: 'POST',
url: 'hello.php',
data: { name: "saurabh" },
success : function(msg) {
// here is the code that will run on client side after running clear.php on server
// function below reloads current page
alert(msg);
}
});
});
PHP脚本:
<?php
$fp = fopen('/Users/saurabh/Desktop/connections.txt', 'w');
echo "Saving file";
fwrite($fp, "hello");
//echo $_POST['yourarray']);
fclose($fp);
?>
答案 0 :(得分:0)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$.ajax({
type: 'POST',
url: 'hello.php',
data: { name: "saurabh" },
success : function(msg) {
alert(msg);
}
});
});
</script>
我刚刚在我的localhost上运行它,它运行正常。你必须在onclick函数上犯一些错误。做这样的事情......
$(document).ready(function(){
$("#someButton").click(function(){
$.ajax({
type: 'POST',
url: 'hello.php',
data: { name: "saurabh" },
success : function(msg) {
alert(msg);
}
});
});
});