与php的ajax连接在子目录下不起作用 [英] ajax connection with php not working at subdirectory

查看:73
本文介绍了与php的ajax连接在子目录下不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我的目录中有一个javascript文件

Hello I have a javascript file in the directory

root/asset/js/a.js

root/ assets/ js / a.js

和目录中的php文件

root/php/foo.php

root/php/foo.php

在a.js文件中,我尝试使用ajax以便与foo.php连接.如果foo.php位于根目录下

At the a.js file I am trying to use ajax in order to connect with the foo.php . If the a foo.php is located at the root directory a.k.a

root/foo.php 

该功能工作正常,但是当我将文件放在上面的目录中时,没有任何效果.这是与连接关联的功能

the function works fine but when i put the file in the directory above nothing works. Here is the function associated with the connection

if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
           alert(xmlhttp.responseText);
        }
    }

    xmlhttp.open("GET","../../studentSignupPHP/test.php?q=" + str, true);

 xmlhttp.open(..)

对我来说似乎很好,所以我无法理解我做错了什么.你能帮我吗?

seems fine to me,so I cannot understand what I am doing wrong. Can you help me?

foo.php

<?php echo "test"; ?>

推荐答案

而不是从js目录遍历,而是像这样使用对根路径的引用

Rather than traversing from the js directory, use a reference to the root path like so

/php/foo.php

/php/foo.php

有关原因,请参见上面charlietfl的评论.

See charlietfl's comment above for the reason why.

已更新@charlietfl的评论

",该路径取决于您在浏览器中加载的页面,而不是js文件的存储位置.打开浏览器开发工具并查看网络"标签,将看到浏览器尝试用于该路径的路径.ajax调用"

这篇关于与php的ajax连接在子目录下不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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