![]() |
responseText is empty?
Dear friends:
I am working through The Book of JavaScript by thau! and I got stuck near the end. In chapter 16, it is discussing making an-synchronized calls, using a server side .php (which I am running from my own machine locally). now the query.php works correctly when I run it from localhost, but the problem is that when I call it from my html page, the responseText is empty. This goes for both IE and FireFox. BTW, I'm running Win7 64Bit, and I am using the latest XAMPP with everything installed This problem was compounded by the fact that the book assumes that google suggestions is still in beta testing, however it is now standard google. It wanted me to use "www.google.com/complete/search?js=true&qu=" which no longer exists, I found out that I have to use "www.google/search?js=true&q=" (see the key is no longer "qu", it is now "q") Having figured that out you'd think I had it in the bag. However.... Here is my query.php, as I said, it runs fine by itself when I type 'localhost/query.php?word=Spidey' into my browser: Code:
<?phpCode:
<html>Does anyone have any suggestions? Thank you for your time. notes: this should work like Google's own search box, i.e. a list should drop down with several suggestions all beginning with the text typed on the left, with the number of results for that suggestion to the right. |
I would suggest that you get an alternative book on JavaScript to learn from as the second edition of that book is a history book on how JavaScript worked eight plus years prior to when it was written and has nothing in it whatever that relates to current JavaScript coding.
The actual cause of your error is that you need an extra line near the top of the PHP before it echos anything to set the content type to text header('Content-type: text/plain'); Without that the processing doesn't know to load the result into responseText as it doesn't know whether the PHP has generated XML, text or something else and so doesn't load either responseText or responseXML. |
Thank you for the quick reply.
I saw this book at my local library (2nd edition like you guessed) and just snatched it up; it is the only javascript book they had. Most of it still applies, I did manage to get to chapter 16 without issues (save that Google Chrome was choking on it, I have now switched to FireFox). I will be looking for another more up to date JavaScript book to continue my studies. Now then: that header you suggested causes the responseText to be "undefined". however, Code:
header("Access-Control-Allow-Origin: *");now my issue is the line: Code:
var the_results = eval(the_response);For some reason, I am getting the entire source of the page back from Google, when I just want the search results. Also, when using the real Google, the number of results per suggestion is no longer returned. The response was expected to be "suggestion1=numResults&suggestion2=numResults" or something like that so that I could split it out; it just does not work that way anymore. The "js=true" does not seem to make a difference either, the response is the same with and without it. I think I got what I could from this, I just have to move on. If I was requesting something from my own server, I think I could make the responseText usable and hopefully without needing eval(), which chapter 17 will get into. Of course, writing a script to talk to any outside/third party website would be tough, there is no easy way to allow for changes made to their site, unless standards are adhered to (like using the "login using facebook" feature on a site outside of facebook). Again, thank you for the fast reply. |
Quote:
If you think in terms of tools then the book is giving you the equivalent of how to make a stone axe - which will still work but is nowhere near as good as a steel one. Statements in JavaScript that used to be used but which now serve no purpose and which should be replaced by more modern equivalents include: document.write alert eval The header I suggested was the only one that did make any difference when I was writing tutorials on how to retrieve information from the server but that may be due to the way the server is configured. Its a pity in some ways that I didn't have the time to write the replacement fot that book that the publishers actually asked me to write when they realised how outdated the new version of the book was. |
First, Felgall, I want to thank you, I will be using your site extensively for a while. And from now on I will always separate my javascript out into a .js file. It makes a lot of sense, why go through so much HTML when editing to find the javascript?
Onto my questions: Ex. 9 Code:
var name;Code:
if(true)Another question: I'm used to naming functions like so: Code:
function name()I take it that in Ex. 15 on your site you are saying that more proper way to name them this way: Code:
name = function()Code:
request.onreadystatechange = function()Last note: I find your examples just a little hard to read, I generally do not: Code:
if(true){Code:
if(true)Code:
if(true){do this; do that;}Ex.22:I love that we now have .getFullYear for the Date() object...no more need for a Y2K fixer function that is needed for the .getYear method Is using <!-- and // --> for hiding JavaScript from those browsers that cannot handle it deprecated? Though I would imagine that by pulling the JavaScript out into a .js file makes it unnecessary anyways; and feature sensing makes it unnecessary as well. Other than general spelling errors, I find your site VERY informative. Thank you for putting it together. Sorry, didn't feel like this absolutely required a new thread.... |
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
|
lol so many typos...
I still love your site though, it has just become my primary resource. You have been very helpful here, everyone else was soooo sarcastic in my other threads. Thank you again!! |
Quote:
|
| All times are GMT +1. The time now is 04:13 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.