Ajax:尝试使用相对路径时出现发布错误

花园

努力获取 Ajax post 请求的相对路径以获取 php 文件。我没有收到错误只是什么也没发生。

浏览了此站点,但找不到我理解的有关 Ajax 相对路径的先前答案。在这方面仍然是新手。真的很感激,如果有人能用外行的术语解释它。

我正在尝试从根文件“index.php”(该文件包含 Ajax 请求)访问 php 文件“search/search.php”。这在两个文件都在同一目录中时起作用。

文件结构如下:

项目文件夹结构

JQuery 代码片段:

$(function() {
  $('form').on("submit", function(e) {
    e.preventDefault();
    $('#error').text(""); // reset
    var name = $.trim($("#search").val());
    if (name.match(/[^a-zA-Z0-9 ]/g)) {
      $('#error').text('Please enter letters and spaces only');
      return false;
    }
    if (name === '') {
      $('#error').text('Please enter some text');
      return false;
    }
    if (name.length > 0 && name.length < 3) {
      $('#error').text('Please enter more letters');
      return false;
    }

    $.ajax({
      url: 'search/search.php',
      method: 'POST',
      data: {
        msg: name
      },
      dataType: 'json',
      success: function(response) {

      $(".content").html("")
      $(".total").html("")

        if(response){
          var total = response.length;
          $('.total') .append(total + " Results");
         }

        $.each(response, function() {
          $.each($(this), function(i, item) {

            var mycss = (item.Type == 1) ? ' style="color: #ffa449;"' : '';
            $('.content').append('<div class="post"><div class="post-text"> ' + item.MessageText + ' </div><div class="post-action"><input type="button" value="Like" id="like_' + item.ID + '_' + item.UserID + '" class="like" ' + mycss + ' /><span id="likes_' + item.ID + '_' + item.UserID + '">' + item.cntLikes + '</span></div></div>');
          });
        });
      }
    });
  });
});
蒂姆·莫顿

前导斜杠仅表示“从文档根开始”,这是存在的地方index.php所以/search/search.php应该是正确的。如果服务器无法找到该文件,则按理说一定发生了一些 url 重写。

您只需将浏览器指向http://localhost:8000/search/search.php. 如果你得到 404,你就知道它与 ajax 无关

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章