Ajax加载页面+ JQuery单击不起作用

时间:2017-12-13 19:08:39

标签: javascript php jquery ajax

为什么我的ajax不起作用?

这些是我的页面。

我正在使用JQuery检查点击事件并使用ajax加载响应者页面。

此代码有什么问题?

index.php

function makeFolders(){

//Get array of folder names (fixed & variable)
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var bottomRow = sheet.getMaxRows()-2;
var folderNames = sheet.getRange(2, 12, bottomRow, 1).getValues();

//If fixed name cell is blank it throws up an alert and stops      
if (folderNames === "") {
    SpreadsheetApp.getUi().alert('Cell A3 is blank.');
}

//If folder ID cell is blank it throws up an alert and stops
var folderUrl = sheet.getRange(3,14).getValue();  
if (folderUrl === "") {
    SpreadsheetApp.getUi().alert('Cell C3 is blank.');
}      

//If fixed name cell and file URL aren't empty, run the program  
if (folderNames !== "" && folderUrl !== "") {    
    //Get ID from folder URL
    var folderId = folderUrl.match(/[-\w]{25,}/);    
    //Get master folder 
    var getRootFolderId = DriveApp.getFolderById(folderId);

    //Copy master n times and rename each copy with the new name
    //stops when it comes to a blank cell 
    for (n=0;n<bottomRow;n++){
          if(folderNames[n] == ""){break;}

          var newFolder = getRootFolderId.createFolder(folderNames[n]);
    }
    ss.toast("Your folders have been made! :)","Finished",3);
}
}**strong text**

响应者页面:

res.php:

<html>
  <head>
    <title>Test</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
      $(document).ready(function() {
          $('#clicket').click(function() {
            $.ajax({
              url: 'res.php?rand=' + Math.random(),
              type: 'GET'
              success: function(results) {
                alert(results);
              }
            });
          });
        });
      </script>
    </head>
  <body>
    <button id="clicket">Hi</button>
  </body>
</html>

更新 我更改了上面的javascript代码。

2 个答案:

答案 0 :(得分:1)

准备好你的逻辑文件。由于您的脚本在您的脑海中,因此正文中的标记尚未加载到DOM中。为了使您的脚本等到构建DOM之后,将逻辑放在文档中,准备将其延迟到那个时间。

$(document).ready(function() {
  $('#clicket').click(function() {
    $.ajax({
      url: 'res.php?rand=' + Math.random(),
      type: 'GET',
      success: function(results) {
        alert(results);
      }
    });
  });
});

答案 1 :(得分:-3)

尝试使用完整网址进行ajax调用。如果您使用的是实时服务器,请填写完整的域名,例如www.your_domain.com/res.php?rand=