[英]How to access a .php file in a different directory using JavaScript?
首先,這個問題可能已經有了答案,但是我整天都在搜索,但是找不到適合我的確切問題的鍛煉方法。
我是JavaScript和AJAX的新手
我正在嘗試在目錄C:\\xampp\\htdocs\\projects\\aptana\\testapp\\
一個名為calendar.php
的Web應用程序。 這有一個script
名為calendar_script.js
在目錄C:\\xampp\\htdocs\\projects\\aptana\\testapp\\scripts\\
。 calendar_script.js
具有功能getDetails()
//--function to get the details from the server--
function getDetails(dateName){
request = createRequest();
if(request == null){
alert("Undable to create request. Please Check Internet Connection or contact service Administrator.");
return;
}
var url = "getEvents.php?dateID=" + escape(dateName);
request.open("GET", url, true);
request.onreadystatechange = displayDetails;
request.send(null);
}
createRequest()
函數返回一個請求對象。 當網頁上有一個單擊事件(即日歷getDetails()
時,將調用getDetails()
函數。
問題出在getEvents.php
。 如果將其放置在目錄C:\\xampp\\htdocs\\projects\\aptana\\testapp\\
一切正常,但是如果我將此文件移動到另一個文件夾,即C:\\xampp\\htdocs\\projects\\aptana\\testapp\\internals\\
並嘗試通過在我的JavaScript代碼中創建文件路徑來訪問文件,該文件似乎無效。 我試着做
var base_url = window.location.origin + "\projects\aptana\testapp\internals\";
var url = base_url + "getEvents.php?dateID=" + escape(dateName);
request.open("GET", url, true);
它似乎不起作用。 我還嘗試對整個文件路徑進行硬編碼: C:\\xampp\\htdocs\\projects\\aptana\\testapp\\internals\\
但這似乎也不起作用。
我知道我做錯了,我缺少了一些東西。 我怎么解決這個問題? 請幫忙。 先感謝您。
因此,您在testapp /中有腳本,而在testapp / internals /中有php。 我認為您必須使用以下方式:
var url = "internals/getEvents.php?dateID=" + escape(dateName);
request.open("GET", url, true);
請注意/
而不是\\
。
/
代替\\
。 C:\\xampp\\htdocs\\projects\\aptana\\testapp
)。 您的代碼:
var url = "internals/getEvents.php?dateID=" + escape(dateName);
request.open("GET", url, true);
request.onreadystatechange = displayDetails;
request.send(null);
更改
var base_url = window.location.origin + "\projects\aptana\testapp\internals\";
進入
var base_url = "projects/aptana/testapp/internals/";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.