jQuery-找不到Ajax网址 [英] jQuery - ajax url not found

查看:114
本文介绍了jQuery-找不到Ajax网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我敢肯定,这真的很容易,而且我很愚蠢,但是似乎根本无法解决问题.

Ok, I'm sure this is really easy and I'm being stupid, but just can't seem to get the bottom of it.

我试图从我的js文件"custom.js"中对"helpers.php"中的某些代码进行简单的AJAX调用.但是,我仍然收到404错误,因为我似乎并没有正确遍历文件夹,尽管我确信自己是...

I am trying to make a simple AJAX call to some code in "helpers.php" from my js file called "custom.js". However, I keep getting a 404 error as I don't appear to be traversing the folders correctly, although I'm convinced I am...

我的文件夹结构如下:

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

还有我在JS中使用的代码:

And the code I'm using in 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);
        }
    });
});

但是在控制台中我得到了:

but in the console I get:

The requested URL /includes/helpers.php was not found on this server.

我要去哪里错了,任何指针都表示赞赏...

Where am I going wrong, any pointers appreciated...

推荐答案

您似乎有两个问题:

JavaScript是在文档的上下文中执行的.其影响之一是,与CSS不同,所有URL都是相对于 document 而不是 .js 文件.您有一个 ../太多.

JavaScript is executed in the context of the document. One of the effects of this is that, unlike CSS, all URLs are relative to the document not the .js file. You have one ../ too many.

您正在尝试访问私有PHP文件

html 大概是您网站的DocumentRoot.外部的文件将不会获得URL(如果已获得URL,则WWW上将可见您硬盘上的任何文件).

html is, presumably, the DocumentRoot of your site. Files outside it do not get URLs (if they did, then any file on your hard disk would be visible to the WWW).

您的目录结构表明您的代码组织应使您应该创建/html/ajax/something.php ,其中 include s helpers.php 并在其中调用函数.

Your directory structure suggests that your code organisation is such that you should be creating /html/ajax/something.php which includes helpers.php and calls functions in it.

这篇关于jQuery-找不到Ajax网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆