所以基本上我的主html页面有3个按钮,每个按钮都有链接的不同背景图像。我试图通过ajax发布将该图片的源传递到我的Php文件中,以写入txt文件。
我的JS:
// MAIN
$(function() {
$(".artButton").click(function(){
var artId = $(this).attr("id");
var source = $("#"+artId).css("background-image").replace(/^url\(['"](.+)['"]\)/, '$1');
// DEBUG
console.log(source) //example: http://localhost/crowd.jpg
console.log(typeof source)//example: string
$.ajax({
type: 'POST',
url: 'writeToTxt.php',
data: source,
success: function(response) {
console.log("Succes");
}
});
});
});
我的Php文件:
<?php
$my_file = 'toExpose.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
$data = $_POST['data'];
fwrite($handle, $data);
?>
Chrome控制台导致以下错误: 405 Ajax Post error
请注意,我不是编程专家...更多关于“我需要一些东西,所以我想自己编写”的更多信息。就编码而言,很多方法可能不是最佳实践,但我只需要它即可按预期工作! 非常感谢您的帮助!
注意:一切都在localhost上运行
更新: 这是页面的HTML:
<html>
<link href="style.css" rel="stylesheet" type="text/css"/>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<body>
<header class="navbar">
<div class="container">
<h1>Welcome to oneFRAME</h1>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="Home.html">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item active">
<a class="nav-link" href="#">Library</a>
</li>
<li class="nav-item">
<a class="nav-link" href="LibraryV2.html">Library v2</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">About</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Contact</a>
</div>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
</div>
</header>
<button id="choice1" class="artButton" type="button">Nature</button>
<button id="choice2" class="artButton" type="button">Art</button>
<button id="choice3" class="artButton" type="button">Crowd</button>
<script src="jquery-3.3.1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script src="main_temp.js"></script>
</body>
</html>
答案 0 :(得分:0)
HTTP 405错误表示您的脚本和/或Web服务器存在问题。该错误表示您正在尝试回传一个在服务器上没有必需权限的文件,以从另一个脚本接收Post信息。
答案 1 :(得分:0)
尝试在
之类的对象中传递数据$.ajax({
type: 'POST',
url: 'writeToTxt.php',
data: {data: source},
success: function(response) {
console.log("Succes");
}
答案 2 :(得分:0)
由于在htdocs或www目录中没有写许可权,因此出现错误。
sudo chmod -R 777 /directory
不确定,但我怀疑您的路径一定是http://localhost/example/index.php
更新的代码:
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" src="jquery.min.js"></script>
</head>
<body>
<button id="choice1" class="artButton" type="button" style="background-image:url('http://localhost/Veridocglobalportal2/veridocedu/public/img/edu-logo.png');">Nature</button>
<button id="choice2" class="artButton" type="button">Art</button>
<button id="choice3" class="artButton" type="button">Crowd</button>
<script type="text/javascript">
// MAIN
$(function() {
$(".artButton").click(function(){
var artId = $(this).attr("id");
var source = $("#"+artId);
switch (artId) {
case "choice1":
var source = "http://localhost/crowd.jpg";
break;
case "choice2":
var source = "http://localhost/people.jpg";
break;
case "choice3":
var source = "http://localhost/alone.jpg";
break;
}
// DEBUG
console.log(source) //example: http://localhost/crowd.jpg
console.log(typeof source)//example: string
$.ajax({
type: 'POST',
url: 'writeToTxt.php',
data: {"data":source},
success: function(response) {
console.log("Succes");
}
});
});
});
</script>
</body>
</html>
writeTotxt.php
<?php
$cwd=getcwd();
$data = $_POST['data'];
$myfile = file_put_contents("$cwd/logs.txt", $data.PHP_EOL,FILE_APPEND | LOCK_EX );
答案 3 :(得分:0)
尝试一下
$(function() {
$(".artButton").click(function(){
var artId = $(this).attr("id");
var source = $("#"+artId).css("background-image").replace(/^url\(['"](.+)['"]\)/, '$1');
// DEBUG
console.log(source) //example: http://localhost/crowd.jpg
console.log(typeof source)//example: string
$.ajax({
type: 'POST',
url: 'writeToTxt.php',
data: {source:source},
success: function(response) {
console.log("Succes");
}
});
});
});
答案 4 :(得分:-1)
您遇到客户端错误。 4.x.x都是关于客户端错误的。
这样更改您的ajax请求-
$.ajax({
type: 'POST',
url: 'writeToTxt.php',
data: 'name=xxx&address=xxxx',
success: function(response) {
console.log("Succes");
}
});
用您的帖子数据替换'name = xxx&address = xxxx'。
,在PHP中使用如下方式- $ name = $ _POST ['name']; $ adress = $ _POST ['address'];