Re: AJAX Help - responseXML always NULL



KDawg44 wrot:
On Aug 2, 6:23 pm, KDawg44 <KDaw...@xxxxxxxxx> wrote:
On Aug 2, 6:12 pm, Peter Michaux <petermich...@xxxxxxxxx> wrote:
KDawg44 wrote:
My responseXML is always null on my AJAX call. When I browse directly
to the PHP script I am calling, the XML file shows up just fine.
What is the Content-Type header of the response? It should be "text/xml"
and the document should validate as valid XML.
[...]
The content type is text/xml.

*Are* *you* *really* *really* *sure*? [psf 1.1]

See below.

THe document tree shows up perfect when I browse to the PHP file.

Note: The "document tree" that was meant here is the automagically indented
syntax-highlighted source code in Gecko-based UAs. If it is only plain text
there, the Content-Type header of the response does not fit. MSHTML ignores
the Content-Type header and *always* displays the document tree (as it
regards it XML) if it sees `<?xml ...?>' and no `<html'.

Also, it works perfect in IE 7 but is not working in firefox

<http://www.jibbering.com/faq/faq_notes/clj_posts.html#ps1DontWork>

(which is disheartening and confusing... I am used to that being
the other way around.... :) ).
[...]

[...]
PHP Script on Server Side
<?php

header('Content-Type: text/xml');

You should use spaces for indentation (at least when posting your code),
not tabs. And you should include the encoding declaration as well.

[...]
$xmlDoc = "<?xml version='1.0' encoding='UTF-8'?><XMLData>";

Since there is nothing to expand for PHP here, it should be the other way
around:

$xmlDoc = '<?xml version="1.0" encoding="UTF-8"?><XMLData>';

$result = verifyLogin($username, $pw);
echo $xmlDoc . $result . "</XMLData>";

echo $xmlDoc . $result . '</XMLData>';

BTW, why the uppercase? Note that XML is case-sensitive.

[...]
Client Side:

function login() {
var username = document.loginForm.username.value;
var password = document.loginForm.password.value;

var username = document.forms["loginForm"].elements["username"].value;
var password = document.forms["loginForm"].elements["password"].value;

Better:

function login(f)
{
var es = f.elements;
if (es)
{
var username = es["username"].value;
var password = es["password"].value;
// ...
return false;
}

return true;
}

<form action="..." onsubmit="return login(this)">
...
<input type="submit" ...>
</form>

xmlHttp.onreadystatechange=function() {
if(xmlHttp.readyState==4) {

readyState == 4 does not mean the request was successful; only that the
response has been received completely. Therefore, it should be

if (xmlHttp.readyState==4 && (/^(20)?0$/.test(xmlHttp.status))
{

(Status `0' can occur when this code is applied on file:// URIs.)

var xmlDoc = xmlHttp.responseXML.documentElement; <---- CULPRIT
Works fine in IE, get a 'xmlHttp.responseXML is null' error in FF 3

Your inline comments suck.

Anyhow, WFM in Fx 3.0.1 on WinXP SP3. It did not work here at first because
in PHP I made the typo

header('Content-Type', 'text/xml');

when it must be

header('Content-Type: text/xml');

Also, note that PHP's header() function must be called before any other
output is sent or the call will be silently ignored. Call
error_reporting(E_ALL); in PHP before the header() call to see possible
warnings about that.

if (xmlDoc.getElementsByTagName("login")
[0].childNodes[0].nodeValue == "true") {

textContent or XPath strikes me as being more efficient and less error-prone
here:

if (xmlDoc.getElementsByTagName("login")[0].textContent === "true")

or

if (xmlDoc.evaluate('//login/text()="true"', xmlDoc, null, 0, null)
.booleanValue)

But using JSON instead of XML would probably be even more efficient and
compatible: <http://json.org/>

window.location="main.php"; <---- THIS IS NOT WORKING IN IE
THOUGH, no error console though.....

<http://jibbering.com/faq/#FAQ4_43>

[...]
}
}
}
xmlHttp.overrideMimeType("text/xml"); <-- TRIED WITH AND WITHOUT
THIS IN FF, SAME PROBLEM

WFM if PHP's header() was not called but XHR's overrideMimeType() was.

xmlHttp.open("GET","app/verifyLogin.php?un=" + escape(username) +
"&pw=" + escape(password),true);

Prefer encodeURIComponent() over escape(). The latter is obsolete and
insufficient, use it only as a fallback.

xmlHttp.send(null);
}

Output on Direct Call to Server:

<XMLData>
<login>true</login>
</XMLData>

And if I do a 'View Source' on that Output:

<?xml version='1.0' encoding='UTF-8'?><XMLData><login>true</login></
XMLData>

Neither does mean the media type declaration was correct, though.
Try e.g. "View Page Info" or LiveHTTPHeaders to be sure:
<https://addons.mozilla.org/addon/3829>


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
.



Relevant Pages

  • Re: receive xml
    ... I can send just XML and not key/value pairs thats ... so long as you set the Content-Type header in the request. ... and PHP can handle that just ... here's a rough sketch of how you might access the XML POST ...
    (comp.lang.php)
  • Re: AJAX Help - responseXML always NULL
    ... to the PHP script I am calling, the XML file shows up just fine. ... the Content-Type header of the response does not fit. ... PHP Script on Server Side ...   function login ...
    (comp.lang.javascript)
  • Re: Ruby performance woes
    ... Now here's my take on all this: PHP and JSP and what I inquired ... S-expressions used as a directly alternate expression of SGML or XML. ... By the way, I've never had access to a Web server that can run ASP, ...
    (comp.lang.lisp)
  • Re: Object Oriented Content System - the idea
    ... >I expected php to be smarter then that. ... >form like bytecode in memory for the next request. ... each action added some stuff to a "response" XML document. ... that map/object/data and formats it for HTML display using an includeed PHP ...
    (comp.lang.php)
  • Re: Need help with PHP DOMXML - get_elements_by_tagname
    ... PHP as a supported platform. ... uses the built in XML handler CF MX provides. ... in newer versions of PHP and the method will *not* support xpath. ...
    (comp.lang.php)