【问题标题】:404 error because of AJAX?由于 AJAX 的 404 错误?
【发布时间】:2015-11-27 14:25:58
【问题描述】:

好的,我不知道如何提出这个问题,但我试图让它简短易懂。
所以我一直在关注this 教程。 (完整代码)

我根据自己的喜好稍微修改了代码,但出现此错误

GET http://sample.com/refresh.php/?lastTimeID=0 404 (Not Found)

(来自浏览器开发者控制台)
我认为这是出错的代码..

var lastTimeID = 0;

$(document).ready(function() {
  $('#btnSend').click( function() {
    sendChatText();
    $('#chatInput').val("");
  });
  startChat();
});

function startChat(){
  setInterval( function() { getChatText(); }, 2000);
}

function getChatText() {
  $.ajax({
    type: "GET",
    url: "/refresh.php?lastTimeID=" + lastTimeID
  }).done( function( data )
  {
    var jsonData = JSON.parse(data);
    var jsonLength = jsonData.results.length;
    var html = "";
    for (var i = 0; i < jsonLength; i++) {
      var result = jsonData.results[i];
      html += '<div>(' + result.chattime + ') <b>' + result.usrname +'</b>: ' + result.chattext + '</div>';
      lastTimeID = result.id;
    }
    $('#view_ajax').append(html);
  });
}

function sendChatText(){
  var chatInput = $('#chatInput').val();
  if(chatInput != ""){
    $.ajax({
      type: "GET",
      url: "/submit.php?chattext=" + encodeURIComponent( chatInput )
    });
  }
}


这就是我感到困惑的地方;它显示http://sample.com/refresh.php/?lastTimeID=0
但是在 refresh.php 之后它在哪里得到“/”?这应该发生吗?与.htaccess 有什么关系?

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /admin/$1/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /admin/index.php/$1 [L]


如何解决此错误?
就像我说的,我不知道如何正确地问这个问题。
还有一个小问题;查看浏览器开发者控制台是否合适?
如果您需要更多信息,请随时询问!
提前致谢。

【问题讨论】:

  • 你有配置htaccess吗?它的内容是什么?
  • 404 失败与变量 pass(?lastTimeID=0) 无关。如果它也产生404错误,你是否尝试直接从url栏加载sample.com/refresh.php/?lastTimeID=0。如果已安装,还可以共享 .htaccess 代码
  • @YPS 感谢您的快速响应。如果我加载 sample.com/refresh.php/?lastTimeID=0 它给了我一个 404 错误。我将添加 .htaccess
  • 我猜你的 htaccess 确实搞砸了,刚才(chrome)我在控制台中写了$.ajax({type: "GET", url: "/refresh.php?lastTimeID=2"}).done(function(){ console.log(arguments);});,它给了我“GET stackoverflow.com/refresh.php?lastTimeID=2&_=1441180776327 404(未找到)”;)试试一条一条地注释掉规则..?
  • GET http://sample.com/refresh.php?lastTimeID=0 refresh.php 后删除 \

标签: php jquery ajax .htaccess


【解决方案1】:

在您的 ajax 请求中添加数据,如下所示:

type: "GET",
url: "/refresh.php",
data: {  
  lastTimeID:lastTimeID
},

...这将使您的函数看起来像这样:

替换全部功能。

function getChatText() {
  $.ajax({
    type: "GET",
    url: "http://sample.com/refresh.php",
    data: {  
      lastTimeID:lastTimeID
    },
  }).done( function( data )
  {
    var jsonData = JSON.parse(data);
    var jsonLength = jsonData.results.length;
    var html = "";
    for (var i = 0; i < jsonLength; i++) {
      var result = jsonData.results[i];
      html += '<div>(' + result.chattime + ') <b>' + result.usrname +'</b>: ' + result.chattext + '</div>';
      lastTimeID = result.id;
    }
    $('#view_ajax').append(html);
  });
}

【讨论】:

  • 以后,请编辑您的答案以添加更多信息,而不是发布多个答案。
【解决方案2】:

所以在 EricG 的帮助下,我禁用了导致问题的整个 .htaccess,这意味着那里有一行你应该尝试逐条评论。我不需要使用.htaccess。但是你应该考虑检查你的 .htaccess (或者如果不使用就禁用它)

感谢 EricG 和所有其他试图帮助我的人!

【讨论】:

  • 无法估计删除 .htaccess 的使用或影响 :) 很高兴我能提供帮助!
猜你喜欢
  • 2015-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-27
  • 1970-01-01
  • 2017-12-05
  • 2016-05-18
相关资源
最近更新 更多