我一直坚持这个问题已经有一段时间了,我很确定它一定很简单,希望有人可以解决这个问题。
所以,我目前正在使用jQuery UI的Autocomplete插件来引用和外部PHP,它从数据库(在数组中)获取信息并将其发送到JSON输出。
当我这样做时,从我的PHP文件(search.php):
echo json_encode($items);
我的输出(当查看search.php文件时)是:
["Example 1","Example 2","Example 3","Example 4","Example 5"]
根据jsonlint.com,这是有效的JSON
问题在于,当我使用jQuery UI的自动完成脚本来引用外部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>
的jQuery
$(function() {
$( "#input" ).autocomplete({
source: "http://www.example.com/search.php",
minLength: 2
});
});
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);
结果
在浏览器中,当信息输入#input
时GET http://www.example.com/search.php?term=Example+1 404 (Not Found)
更新:真正的PHP网址在这里:http://www.qwota.co.uk/wp/wp-content/themes/qwota/list-comments.php?term=Your
请帮忙!
更新:答案
Majid Fouladpour指出了我的问题的答案
问题不在于我的代码,而在于尝试使用WordPress的'$ wpdb全局变量(据我所知)它包含了它自己的标题,而且通常布局之外的任何内容都会导致404错误,即使文件实际存在。
我目前正试图通过创建自己的MySQL请求而不使用WordPress的全局变量/标题来解决问题。
PS。马吉德,一旦StackOverflow让我,我会回来给你一个'有用的打勾'! (我还是一个n00b。)
答案 0 :(得分:1)
您确定路径来源:“http://www.example.com/search.php”是否正确?
答案 1 :(得分:1)
您必须确保目标网址存在。如果你真的使用http://www.example.com/search.php那么,它就不存在了,所以这就是为什么它不起作用。
<强>更新强>
由于您有一个真正有效的网址(我测试了它!),您可以采取以下几个步骤:
答案 2 :(得分:1)
我认为包含是问题所在。正如马吉德指出的那样......请改用以下内容。
include("../../../wp-load.php");
祝你好运!
答案 3 :(得分:0)
您的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
回复正文
["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 Not Found
。有人可能会对此进行调查并提供潜在的理由和解决方案。