我正在做一个简单的项目,其中有一个javascript文件和一个php文件。我正在尝试使用ajax来调用一些php代码,然后这些代码将在灯箱上显示文本。我的问题是我收到以下错误:
这个错误指向我的php文件。
"NetworkError: 404 Not Found - http://localhost/folder/folder/dictionary.php"
我已经检查了(并反复检查)我所有的代码,但我不知道如何修复这个错误。有人能帮帮我吗?我将在下面提供我的JavaScript和PHP代码:
JavaScript:
$(document).ready(function() {
$("#SearchField").submit(function(e) {
e.preventDefault();
// grabs all post variables in the form
var info = $(this).serialize();
$.post('dictionary.php', info, function(data) {
$('.DictionaryContentWrapper').append(data);
});
// prevent server from refreshing page
return false;
});
});
PHP:
$input = $_POST['input'];
echo $input;
目录结构:
index.php
Widget
- **input.js**
- getURL.php
- **dictionary.php**
lightbox
- lightbox.css
- lightbox.js
一个简单的解决方案是使用相对于web服务器根的绝对路径。
类似于:
$.post('/Widget/dictionary.php', info, function(data) {
然而,这假设您的index.php
位于网站的根目录http://localhost/index.php
中,但从您发布的信息来看,这一点并不十分清楚:错误消息似乎不符合目录结构。
编辑:如果您从index.php
调用脚本,您也可以使用:
$.post('Widget/dictionary.php', info, function(data) { // path relative to index.php