簡體   English   中英

錯誤404和jQuery Autocomplete JSON引用外部PHP文件

[英]Error 404 with jQuery Autocomplete JSON referencing external PHP file

我在這個問題上停留了一段時間,而且我敢肯定,它一定很簡單,希望在那里的人能有所作為。

因此,我目前正在使用jQuery UI的Autocomplete插件來引用和外部PHP,該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。 Majid,一旦StackOverflow讓我回來,我會回來給您一個“有用的提示”! (我仍然是n00b。)

您確定路徑來源:“ http://www.example.com/search.php”正確嗎?

您必須確保目標URL存在。 如果您真的在使用http://www.example.com/search.php,那么它根本不存在,所以這就是為什么它不起作用的原因。

更新

由於您有一個有效的真實網址(我已經對其進行了測試!),因此可以采取以下步驟:

  1. 確保沒有錯字。 如果有,請修復它。
  2. 確保可以從瀏覽器打開該URL。 如果不能,則可能是網絡訪問問題(防火牆,代理,服務器權限問題等)。
  3. 嘗試確保重定向到另一個已知的URL。 404錯誤實際上是“找不到”錯誤。 別無其他。

我認為包含是問題。 正如Majid指出的那樣,請改用以下包含。

include("../../../wp-load.php");

祝好運!

您的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 可能有人可以對此進行調查並提供潛在的原因和解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM