最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - relative path for open method of a XMLHttpRequest object - Stack Overflow

programmeradmin2浏览0评论

In the JavaScript file containing the Ajax request what is the URL relative to? I've got the www directory containing the directories alpha and bravo. The JavaScript file in alpha and the HTML file that includes it and the PHP that processes the request in bravo.

In the JavaScript file I have xmlhttp.open("GET", "CheckServer.php?name="+name,true); but I don't think CheckServer.php is right. I've tried ../bravo/CheckServer.php but it doesn't work.

I'm not using JQuery and I am using WAMP.

Plus, is there any trouble shooting tools I can use to see if the PHP page for processing the request is being accessed in the first place?

EDIT: I opened the console and it says the function I'm calling in the JavaScript file is not defined. This only happens when I moved the .js file to a different directory. (I modified the <script> tag appropiratly: <script type="text/javascript" src="../alpha/Check.js">.

EDIT 2: I think there is a problem with WAMP because I copy the exact same files/folders to the desktop and everything works.

In the JavaScript file containing the Ajax request what is the URL relative to? I've got the www directory containing the directories alpha and bravo. The JavaScript file in alpha and the HTML file that includes it and the PHP that processes the request in bravo.

In the JavaScript file I have xmlhttp.open("GET", "CheckServer.php?name="+name,true); but I don't think CheckServer.php is right. I've tried ../bravo/CheckServer.php but it doesn't work.

I'm not using JQuery and I am using WAMP.

Plus, is there any trouble shooting tools I can use to see if the PHP page for processing the request is being accessed in the first place?

EDIT: I opened the console and it says the function I'm calling in the JavaScript file is not defined. This only happens when I moved the .js file to a different directory. (I modified the <script> tag appropiratly: <script type="text/javascript" src="../alpha/Check.js">.

EDIT 2: I think there is a problem with WAMP because I copy the exact same files/folders to the desktop and everything works.

Share Improve this question edited Feb 13, 2013 at 22:47 Celeritas asked Feb 13, 2013 at 21:50 CeleritasCeleritas 15.1k39 gold badges121 silver badges203 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

It is relative based on the current location of the page it is called on. It has nothing to do with where the JavaScript is loaded from.

Open up the console and look at the Ajax request [console or net tab], you will see the path that it is requesting.

It's an URL, those have nothing to do with directories per se. They might be mapped to directories on the server and often are but the client can never know it for sure and doesn't care. The URL is relative to the current URL (what you see in your browser address bar).

So, the question is not "where is CheckServer.php located on the server", but "how can I access it from the client".

If it's like:

http://example./alpha/index.html
http://example./bravo/CheckServer.php

fine. Use the relative URL.

But if it's like:

http://alpha.example./index.html
http://bravo.example./CheckServer.php

then it gets plicated. You will have to look into CORS (Cross-Origin Resource Sharing) because AJAX usually does not work across different domains.

Oh, and if CheckServer.php is not accessible at all... you probably can imagine the answer.

发布评论

评论列表(0)

  1. 暂无评论