jQuery自动完成JSON引用外部PHP文件时出现错误404
我在这个问题上被困了一段时间,我很确定这一定是一个非常简单的问题,希望有人能给我一些启示 因此,我目前正在使用jQueryUI的Autocomplete插件来引用和外部PHP,PHP从数据库(在数组中)获取信息并将其发送到JSON输出 执行此操作时,请从我的PHP文件(search.PHP)中:jQuery自动完成JSON引用外部PHP文件时出现错误404,php,mysql,json,jquery-ui,autocomplete,Php,Mysql,Json,Jquery Ui,Autocomplete,我在这个问题上被困了一段时间,我很确定这一定是一个非常简单的问题,希望有人能给我一些启示 因此,我目前正在使用jQueryUI的Autocomplete插件来引用和外部PHP,PHP从数据库(在数组中)获取信息并将其发送到JSON输出 执行此操作时,请从我的PHP文件(search.PHP)中: echo json_encode($items); 我的输出(查看search.php文件时)如下: 根据jsonlint.com,这是有效的JSON 问题是,当我使用jQuery UI的自动完成脚本
echo json_encode($items);
我的输出(查看search.php文件时)如下:
根据jsonlint.com,这是有效的JSON
问题是,当我使用jQuery UI的自动完成脚本引用external search.php文件时,Chrome只会给我以下错误:
GET http://www.example.com/search.php?term=my+search+term 404 (Not Found)
我已经尝试将JSON代码直接输入到jQuery中的“Source:”声明中,这很好,但它不会从外部PHP文件读取JSON
有人能帮忙吗
这是我的密码:
HMTL
<p class="my-input">
<label for="input">Enter your input</label>
<textarea id="input" name="input"
class="validate[required]"
placeholder="Enter your input here.">
</textarea>
</p>
PHP
header("Content-type: application/json");
// no term passed - just exit early with no response
if (empty($_GET['term'])) exit ;
$q = strtolower($_GET["term"]);
// remove slashes if they were magically added
if (get_magic_quotes_gpc()) $q = stripslashes($q);
include '../../../my-include.php';
global $globalvariable;
$items = array();
// Get info from WordPress Database and put into array
$items = $wpdb->get_col("SELECT column FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY column ASC");
// echo out the items array in JSON format to be read by my jQuery Autocomplete plugin
echo json_encode($items);
结果
在浏览器中,将信息键入#输入时
更新:真正的PHP url在这里:
请帮忙
更新:回答
Majid Fouladpour已经指出了我问题的答案
问题不在于我的代码,而在于试图使用WordPress的$wpdb全局变量,因为(据我所知)它包含自己的头文件,并且任何超出其通常布局的内容都将导致404错误,即使文件确实存在
我目前正试图通过创建自己的MySQL请求,而不是使用WordPress的全局变量/头来解决这个问题
马吉德先生,我会回来给你一个“有用的记号”,只要我同意!(我仍然是n00b。)您确定路径源:http://www.example.com/search.php“正确吗?您必须确保目标URL存在。如果你真的在使用wk,它根本不存在,所以这就是它不起作用的原因 更新 由于您有一个真正有效的URL(我已经测试过了!),以下是您可以采取的几个步骤:
您的apache服务器正在发送错误的头。以下是一对请求和响应: 请求
GET /wp/wp-content/themes/qwota/list-comments.php?term=this HTTP/1.1
Host: www.qwota.co.uk
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Cookie: __utma=142729525.1341149814.1305551961.1305551961.1305551961.1; __utmb=142729525.3.10.1305551961; __utmc=142729525; __utmz=142729525.1305551961.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
HTTP/1.1 404 Not Found
Date: Mon, 16 May 2011 13:28:31 GMT
Server: Apache
X-Powered-By: PHP/5.2.14
X-Pingback: http://www.qwota.co.uk/wp/xmlrpc.php
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Pragma: no-cache
Last-Modified: Mon, 16 May 2011 13:28:31 GMT
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8
响应标题
GET /wp/wp-content/themes/qwota/list-comments.php?term=this HTTP/1.1
Host: www.qwota.co.uk
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Cookie: __utma=142729525.1341149814.1305551961.1305551961.1305551961.1; __utmb=142729525.3.10.1305551961; __utmc=142729525; __utmz=142729525.1305551961.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
HTTP/1.1 404 Not Found
Date: Mon, 16 May 2011 13:28:31 GMT
Server: Apache
X-Powered-By: PHP/5.2.14
X-Pingback: http://www.qwota.co.uk/wp/xmlrpc.php
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Pragma: no-cache
Last-Modified: Mon, 16 May 2011 13:28:31 GMT
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8
响应主体
$(function() {
$( "#input" ).autocomplete({
source: "http://www.example.com/search.php",
minLength: 2
});
});
["Bake 'em away... toys.","Content precedes design. Design in the absence of content is not design, it\u2019s decoration.","Hanging on in quiet desperation is the English way.","I'm a reasonable man, get off my case.","Look at me, Damien! It's all for you!","Never get out of the boat... absolutely god damn right.","That gum you like is going to come back in style.","The secret to creativity is knowing how to hide your sources.","Things could be different... but they're not.","Your eyes... they turn me."]
因此,即使您收到来自服务器的返回响应,在头文件中也没有找到
HTTP/1.1 404。有人可能会对此进行调查,并提供一个潜在的原因和解决方案。我认为包括在内就是问题所在。正如马吉德指出的。。。请使用下面的include
include("../../../wp-load.php");
祝你好运 我猜www.example.com与您发送ajax请求的域不同。尝试使用浏览器访问此地址:一切似乎都很好。您好。。。这是真实的文件:自动完成页面的url是什么?这比搜索脚本的url更有用,因为可以在那里检查您是否使用绝对或相对路径,以及路径是否正确。可以在此处找到自动完成表单输入的url:-单击页面右上角的“添加报价”以获取表单感谢您的回复。。。但是没有,真正的文件确实存在,URL是100%正确的。您好。感谢您回复我。1,URL中没有输入错误2,我可以在浏览器中打开该文件,并让它回显正确的JSON数据3,我尝试将源代码重定向到另一个文件,但我得到“XMLHttpRequest无法加载…”。。。“Access Control Allow Origin”错误不允许,因为该文件未设置为放置我需要的信息(我想)。但至少它看起来是在正确的地方。还有其他建议吗?非常感谢你的帮助。非常感谢你提供的信息。你能告诉我下一步该怎么做吗?你认为404是我的代码出了问题,还是其他什么问题?这似乎是Word Press特有的问题。我在谷歌上搜索了False404标题,这两个结果读起来很好:,.Hmm。。。你可能就在那里。非常感谢你的推荐,我会深入了解你的推荐,看看能找到什么。我会把我的发现发回来。