开发者

Error 404 with jQuery Autocomplete JSON referencing external PHP file

I'm been stuck on this problem for a while and I'm pretty sure it must be something quite simple that hopefully someone out there can shed some light on.

So, I'm currently using jQuery UI's Autocomplete plugin to reference and external PHP which gets information from a database (in an array) and sends it to a JSON output.

From my PHP file (search.php) when I do this:

echo json_encode($items);

My output (when looking at the search.php file) is this:

["Example 1","Example 2","Example 3","Example 4","Example 5"]

Which is valid JSON according to jsonlint.com

The problem is that when I use jQuery UI's Autocomplete script to reference the external search.php file, Chrome just gives me the following error:

GET http://www.example.com/search.php?term=my+search+term 404 (Not Found)

I have tried inputting the JSON code straight into the 'Source:' declaration in my jQuery, and this works fine, but it will not read the JSON from the external PHP file.

Please can someone help?

Here's my code:

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);

Result

In browser, when information is typed into #input

GET http://www.example.com/search.php?term=Example+1 404 (Not Found)

Update: the real PHP url is here: http://www.qwota.co.uk/wp/wp-content/themes/qwota/list-comments.php?term=Your

Please help!

UPDATE: ANSWER

The answer to my problem has been pointed out by Majid Fouladpour

The problem wasn't with my code but rather with trying to use WordPress' $wpdb global variable as (as far as I understand) it includes it's own headers, and anything开发者_Python百科 outside of it's usual layout will result in a 404 error, even if the file is actually there.

I'm currently trying to get around the problem by creating my own MySQL requests and not using WordPress's global variables / headers.

PS. Majid, I'll come back and give you a 'helpful tick' once StackOverflow lets me! (I'm still a n00b.)


Are you sure the path source: "http://www.example.com/search.php" is correct?


You have to make sure that the target URL exists. If you are really using http://www.example.com/search.php then, wk, it simply does not exist, so this is why it does not work.

Update

Since you have a real URL that's working (I tested it!), here are a few steps you can take:

  1. Make sure there's no typo. If there's one, fix it.
  2. Make sure you can open that URL from your browser. If you cannot, then you might be having network access problems (firewall, proxy, server permission issues, etc.)
  3. Try redirecting to another know URL, just to make sure. The 404 error is really a "not found" error. It cannot be anything else.


I think the include is the issue. As Majid pointed out... use the below include instead.

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

Good luck!


Your apache server is sending wrong headers. Here is a pair of request and response:

Request

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)

Response headers

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

Response body

["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."]

So, even though you receive back response from the server, it has HTTP/1.1 404 Not Found in the headers. Someone may be able to investigate this and provide a potential reason and solution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜