Php 405 Ajax POST错误

Php 405 Ajax POST错误,php,ajax,post,http-status-code-405,Php,Ajax,Post,Http Status Code 405,所以基本上我的html主页有3个按钮,每个按钮都有一个不同的背景图片链接到它。我正试图通过ajax post将此图像的源代码传递到我的Php文件中,以写入txt文件 我的JS: // MAIN $(function() { $(".artButton").click(function(){ var artId = $(this).attr("id"); var source = $("#"+artId).css("background-image").replace(/^url

所以基本上我的html主页有3个按钮,每个按钮都有一个不同的背景图片链接到它。我正试图通过ajax post将此图像的源代码传递到我的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控制台导致以下错误:

请注意,我不是编程专家。。。更多关于我需要的东西,所以我试着自己编码。很多这可能不是编码方面的最佳实践,但我只是需要它像我期望的那样工作! 非常感谢你的帮助

注意:所有内容都在本地主机上运行

更新: 这是页面的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>

HTTP 405错误表示脚本和/或web服务器存在问题。此错误表示您试图传回的文件在服务器上没有从其他脚本接收Post信息所需的权限。

您收到的是客户端错误。 4.x.x都是关于客户端错误的

像这样更改您的ajax请求-

$.ajax{ 键入:“POST”, url:'writeToTxt.php', 数据:'name=xxx&address=xxxx', 成功:功能响应{ console.logsuccess; }
}; 尝试以类似对象的方式传递数据

$.ajax({  
            type: 'POST',  
            url: 'writeToTxt.php', 
            data: {data: source},
            success: function(response) {
            console.log("Succes");
    }

您收到一个错误,因为您在htdocs或www目录中没有写入权限

使用sudo chmod-R 777/目录更改文件夹权限 更改您的ajax方法,使其在某些情况下无法使用。 不确定,但我怀疑你的道路是否正确

更新代码:

writeTotxt.php

试试这个

$(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");
        }
     });
  });
});

是laravel吗?请检查“toExpose.txt”文件的权限。HTTP 405错误表示脚本和/或web服务器存在问题。此错误表示您正试图传回一个文件,该文件在服务器上没有从其他脚本接收帖子信息所需的权限。您正在使用ubantu?您的web服务器权限是什么。在目录上发出ls-lh并向我们发送权限。您可能需要对目录和文件发出chown和chmod。您的问题缺少信息,例如是否存在框架?只是作为说明添加,说明所有内容都在本地主机上运行。txt文件到目前为止还不存在,因为我认为php会创建一个完整的txt文件。我将php的url更改为。但我仍然得到405错误。关于权限,当我在windows localhost上工作时,我已更改了所有文件所在文件夹的权限,以允许所有更改。文件是否位于位置?@Stas06 artButton是按钮还是图像?var source=$+artId.cssbackground image.replace/^url['.+[']/,'$1';路径是什么?artButton是我给按钮使用的一个类。jsjQuery代码中的这个似乎是正确的,您需要检查服务器文件设置。查看allow方法验证后,首先检查writeToTxt.php权限。在这里找到
   <?php
$cwd=getcwd();
$data = $_POST['data'];
$myfile = file_put_contents("$cwd/logs.txt", $data.PHP_EOL,FILE_APPEND | LOCK_EX );
$(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");
        }
     });
  });
});