jQuery - ajax的url未找到。

3

好的,我相信这很容易,但我可能有点傻,似乎无法理解它。

我正在尝试从名为“custom.js”的js文件中对“helpers.php”中的一些代码进行简单的AJAX调用。 但是,我一直收到404错误,因为我似乎没有正确遍历文件夹,尽管我确信我已经做到了...

我拥有的文件夹结构如下:

html
    index.php
    js/
        custom.js
includes
    helpers.php

我正在JS中使用的代码:

$(document).on('ready', function() {
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "../../includes/helpers.php",
        data: { func: "results", days: "7"}, 
        success: function(rows) {
            console.log(rows);
        }
    });
});

但是在控制台中,我得到了以下输出:
The requested URL /includes/helpers.php was not found on this server.

我哪里做错了,非常感谢任何指导意见...


你的上述代码在index.php文件中吗? - Abhik Chakraborty
你尝试使用 ../includes/helpers.php 作为 URL 了吗?我认为那应该可以。 - jeroenvisser101
这可能取决于JS的使用位置,而不是JS实际放置的位置? - Royal Bg
你的JS代码在哪里被引用了?能给出在页面中的位置吗? - Susheel Singh
我在index.php文件中包含了"custom.js"。 - user2672288
7个回答

6
您似乎有两个问题:
JavaScript在文档上下文中执行。其中一个影响是,与CSS不同,所有URL都相对于文档而不是.js文件。您有一个../太多。
您正在尝试访问私有PHP文件。
html可能是您网站的DocumentRoot。它外面的文件没有URL(如果有的话,则硬盘上的任何文件都将可见于WWW)。
您的目录结构表明,您的代码组织应该创建/html/ajax/something.php,它包括helpers.php并调用其中的函数。

谢谢你的回答。我明白你的意思,我也认为如此并编辑了URL为../includes/helpers.php,但我仍然得到404错误。这是代码中有问题还是其他原因?helpers.php文件的权限为644,应该是可以访问的吧? - user2672288
1
@m0atz — 请查看答案的后半部分,开始于“您正在尝试访问私有PHP文件”。 - Quentin
@Quentinincludes/helpers.php是正确的还是错误的?因为js文件已经在index.php中被包含了。 - Susheel Singh
1
@Susheel - 由于 includes 不是 html 的子文件夹,所以这是错误的。 - Quentin
@Quentin 我以为是我的错误 :( - Susheel Singh
显示剩余2条评论

1
将URL更改为:
url: "../includes/helpers.php",

由于您已将js包含在index文件中,因此根路径的路径将相对于index.php,而不是custom.js(希望我的说法不会让人困惑)


现在我明白了,问题出在文件夹结构上。 - user2672288

1

Just like PHP includes, including a Javascript file in the script tag is the same as copy pasting the Javascript code at that part where the

So change it to:

    //Previous code...
    $.ajax({
            type: "POST",
            dataType: "json",
            url: "../includes/helpers.php",

   //Folowing code...


1

您在Ajax中使用的URL是相对于您的index.php文件的。


0

我假设你的js代码是从index.php中调用的,因此在那里执行。

路径应该相对于这个页面。

$(document).on('ready', function() {
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "../includes/helpers.php",
        data: { func: "results", days: "7"}, 
        success: function(rows) {
            console.log(rows);
        }
    });
});

1
你的服务器 documentRoot 是什么? - Romain Braun
2
然后,正如@Quentin所说,你的helpers.php在你的服务器之外。你无法访问它,因为html是根目录。你需要重新思考你的架构。 - Romain Braun
太好了,我现在明白了。非常感谢您的答案和评论。 - user2672288

-1

或者,您也可以使用静态URL:

$(document).on('ready', function() {
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "/includes/helpers.php",
        data: { func: "results", days: "7"}, 
        success: function(rows) {
            console.log(rows);
        }
    });
});

1
从问题中:在此服务器上未找到所请求的URL /includes/helpers.php。 - Quentin

-1
你尝试将 custom.jshelpers.php 放在同一个文件夹中吗?看看是否有效。

为什么他要把它放在同一个文件夹中?他想在客户端和服务器端代码之间进行区分。因此,includes/helpers.php将会起作用。 - Susheel Singh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接