簡體   English   中英

Jquery Ajax 發布但預期 PHP 代碼未運行

[英]Jquery Ajax Post but expected PHP code not running

我正在嘗試在本地服務器上設置一個簡單的網頁。 該網頁有一個按鈕,當在本地客戶端上按下該按鈕時,我希望在服務器上創建一個目錄。 PHP 在服務器端運行,而 jquery 在客戶端運行。

我的 PHP 代碼在 test.php: <?php mkdir('/home/pablo/test_dir', 0777, true); ?> <?php mkdir('/home/pablo/test_dir', 0777, true); ?>

script.js 中的 jquery/ajax:

var toggleOutlet = function(buttonClicked) {
    $.ajax({
        url: 'test.php',
        success: function(data) {
            alert('Directory created!');
        }
    });
};
$(function() {
    $('.toggleOutlet').click(function() {
        toggleOutlet($(this));
    });
});

當我按下客戶端上的按鈕時,我確實收到了“已創建目錄”警報。 但在服務器上沒有創建 test_dir? 我在做什么錯?,為了完整起見:我的 html 是:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>MyWeb</title>
  <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
</head>

<body>

  <div class="container">
    <label>Example Button</label>
    <div class="btn-group btn-group-justified" role="group" aria-label="...">
      <div class="btn-group" role="group">
        <button type="button" data-outletId="1" data-outletStatus="on" class="btn btn-default toggleOutlet">On</button>
      </div>
    </div>

  </div>
</body>
<script src="//code.jquery.com/jquery-3.2.0.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="script.js"></script>
</html>

您好@pablo919 您需要檢查目錄“/home/pablo”是否具有寫入權限,其他可以更改代碼 ajax 並添加 function 錯誤以檢查是什么。

例子:

$.ajax({
        url: 'test.php',
        success: function(data) {
            alert('Directory created!');
        },
        error: function (jqXHR, exception) {
                alert('Uncaught Error.\n' + jqXHR.responseText);
        }

    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM