尝试从Javascript运行本地PHP文件,包括本地和
我有一个Javascript,它使用AJAX get方法调用PHP脚本。如果我在外部运行PHP脚本,它可以正常工作并创建connections.txt文件。但是对于JS,它不起作用尝试从Javascript运行本地PHP文件,包括本地和,php,ajax,post,Php,Ajax,Post,我有一个Javascript,它使用AJAX get方法调用PHP脚本。如果我在外部运行PHP脚本,它可以正常工作并创建connections.txt文件。但是对于JS,它不起作用 $(document).on("click", "#target2", function(){ var arr = ["one","two","three"]; $.ajax({ type: 'POST', url: 'hello.php',
$(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);
?>
你说的“它不工作”是什么意思?开发人员控制台的网络选项卡中是否出现404或500错误?是否将单击绑定包装到就绪函数:$(function(){…});没有,我已经测试了点击,它工作正常。我面临的问题是POST方法。我收到405个错误:[2015-11-26 00:15:36]错误不支持的方法“POST”。localhost--[26/Nov/2015:00:15:36 CET]“POST/hello.php HTTP/1.1”405 300->/hello.phpNo,单击按钮没有问题。我面临的问题是Post函数。我收到以下错误405300:[2015-11-26 00:15:36]错误不支持方法“POST”。本地主机--[26/Nov/2015:00:15:36 CET]“POST/hello.php HTTP/1.1”405 300->/hello.php
<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>
$(document).ready(function(){
$("#someButton").click(function(){
$.ajax({
type: 'POST',
url: 'hello.php',
data: { name: "saurabh" },
success : function(msg) {
alert(msg);
}
});
});
});