我重新组织了我的目录结构,我的 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/