javascript - 无法访问php文件

标签 javascript php directory-structure

我重新组织了我的目录结构,我的 php 文件没有被使用/调用。就好像我无法访问它一样。我的功能和 php 文件在此之前按预期工作,我的文件权限已经整理好,所以有人可以引导我朝着正确的方向前进吗?

目录如下:

public_html(folder)
  - javascript(folder)
    - main(folder)
      - userManagement.js => The `tryEmail` function is in here.
  - php(folder)
    - users-profs(folder)
      - tryEmail.php
  - index.html

代码:

 function tryEmail(email)
    {
        console.log("function:tryEmail, param: " + email);
        return function()
        {
            $.post("../../php/users-profs/tryEmail.php",
            {
                email:email
            },
            function(data)
            {
                console.log("function:tryEmail-post, data: " + data);
                if(data == "valid")
                    return true;
                else
                    return false;
            });
        }
    }

最佳答案

URL ../../php/users-profs/tryEmail.php 将相对于用户当前的 URL 进行解释,而不是 .js 的 URL > 文件。将 $.post() 中的 URL 与您将调用此脚本的 URL 匹配,就可以了。

根据评论编辑: 添加一个斜杠:/php/users-profs/tryEmail.php -- 没有它,它是一个相对 URL,所以它在查找错误的地方。

此外,您需要删除匿名包装函数,因为您不能返回匿名函数,而且无论如何它都不会执行。您的代码应如下所示:

function tryEmail(email)
{
    console.log("function:tryEmail, param: " + email);
    $.post("/php/users-profs/tryEmail.php", // fixed the leading / here
        {
            email:email
        },
        function(data)
        {
            console.log("function:tryEmail-post, data: " + data);
            if(data == "valid")
                return true;
            else
                return false;
        });
}

关于javascript - 无法访问php文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21494902/

相关文章:

php - 如何设计大约 50 列的表并选择用户所需的列

performance - 在文件系统中存储图像的最佳文件夹结构?

javascript - 公共(public)目录中的资源不可用

php - 如何根据两个单元格值进行搜索

python - 如何在不维护 Python 中的目录结构的情况下从 zip 中提取文件?

javascript - 如何在 angular cli 1.0.0-rc.0 中正确包含 jQuery?

javascript - 表单提交后更新页面 - 使用 Angular

javascript - 为什么我的 SVG 中的 .selectAll() D3 创建的圆圈使用 .length 被称为 1 个元素?

javascript - onclick 当ajax响应到达时准备html并在react中渲染?

php - 如何使用fix_mysql.inc.php文件将所有MySql函数替换为Mysqli