473,505 Members | 13,904 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

AJAX Help - responseXML always NULL

Hi,

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.

I have read that if a returned XML file is not valid, it will always
appear as null. I am just returning some basic XML like:

<?xml version='1.0' encoding='UTF-8'?>
<XMLData>
<person>kevin</person>
<person>mac</person>
<person>maltsy</person>
</XMLData>

I do not want to have to write a schema or DTD or anything if
possible. I just want to return this from a script to an AJAX call.

Is this what is causing this to happen? Is there a way around this if
it is?

Thanks.

Kevin
Aug 2 '08 #1
6 9794
KDawg44 wrote:
My responseXML is always null on my AJAX call.
There is no such thing as an "AJAX call". Learn to understand what you do,
and avoid commercial buzzwords.
When I browse directly to the PHP script I am calling, the XML file shows
up just fine.
If it is not displayed as a document tree, then the reason of tghe
`responseXML' property value being `null' is that you are serving it with
the wrong media type. Use text/xml or application/xml. In PHP this can be
achieved with

<?php header('Content-Type: text/xml; charset=utf-8'); ?>
PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Aug 2 '08 #2
KDawg44 wrote:
Hi,

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.

--
Peter
Aug 2 '08 #3
On Aug 2, 6:12*pm, Peter Michaux <petermich...@gmail.comwrote:
KDawg44 wrote:
Hi,
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.

--
Peter
The content type is text/xml. THe document tree shows up perfect when
I browse to the PHP file. Also, it works perfect in IE 7 but is not
working in firefox (which is disheartening and confusing... I am used
to that being the other way around.... :) ).

Thanks.

Kevin
Aug 2 '08 #4
On Aug 2, 6:23*pm, KDawg44 <KDaw...@gmail.comwrote:
On Aug 2, 6:12*pm, Peter Michaux <petermich...@gmail.comwrote:
KDawg44 wrote:
Hi,
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.
--
Peter

The content type is text/xml. *THe document tree shows up perfect when
I browse to the PHP file. *Also, it works perfect in IE 7 but is not
working in firefox (which is disheartening and confusing... *I am used
to that being the other way around.... *:) *).

Thanks.

Kevin
I am still struggling with this, which is not good as I am on a tight
schedule (of course). Here is some exact code that I am using.

PHP Script on Server Side
<?php

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

include(CONNECT TO DB);

.... SOME CUT OUT FOR BREVITY ....

$xmlDoc = "<?xml version='1.0' encoding='UTF-8'?><XMLData>";
$result = verifyLogin($username, $pw);
echo $xmlDoc . $result . "</XMLData>";

.... SOME MORE CUT ....
Client Side:

function login() {
var username = document.loginForm.username.value;
var password = document.loginForm.password.value;
xmlHttp.onreadystatechange=function() {
if(xmlHttp.readyState==4) {
var xmlDoc = xmlHttp.responseXML.documentElement; <---- CULPRIT
Works fine in IE, get a 'xmlHttp.responseXML is null' error in FF 3
if (xmlDoc.getElementsByTagName("login")
[0].childNodes[0].nodeValue == "true") {
window.location="main.php"; <---- THIS IS NOT WORKING IN IE
THOUGH, no error console though.....
} else {
document.getElementByID("loginError").innerHTML =
"<center>Invalid Username and/or Password</center>"; <--- THIS ISN'T
WORKING IN IE EITHER.....
}
}
}
xmlHttp.overrideMimeType("text/xml"); <-- TRIED WITH AND WITHOUT
THIS IN FF, SAME PROBLEM
xmlHttp.open("GET","app/verifyLogin.php?un=" + escape(username) +
"&pw=" + escape(password),true);
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>


Any other thoughts? I am at my wits end.....

Thank you very much for any help.

Kevin
Aug 3 '08 #5
KDawg44 wrot:
On Aug 2, 6:23 pm, KDawg44 <KDaw...@gmail.comwrote:
>On Aug 2, 6:12 pm, Peter Michaux <petermich...@gmail.comwrote:
>>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
Aug 3 '08 #6
On Aug 3, 8:57*am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
KDawg44 wrot:
On Aug 2, 6:23 pm, KDawg44 <KDaw...@gmail.comwrote:
On Aug 2, 6:12 pm, Peter Michaux <petermich...@gmail.comwrote:
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 plaintext
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
Thanks for your suggestions. You make a lot of very good points.

This is suddenly working and I think I figured out why. I am
utilizing Dreamweaver so that I can (hopefully) quickly get the HTML
stuff up (though one would argue that since i think this is what
caused my problem that it definitely did not save time...). When I
slapped a few text boxes on the page, it added a form element. I
think this when the button was pushed that called the script to
execute the AJAX call, it also reloaded the page due to an empty
ACTION attribute on the FORM tag.

Now that I have removed the FORM tag, it appears to be working (well
getting as far as my next problem with my PHP SESSION var... but
that's another story...)

Thanks!

Kevin
Aug 3 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5782
by: | last post by:
In my PHP, argv always return correct value. In a remote server, PHP returns for argv always null. I believe this is a "php.ini" problem but from PHP help I don't found anything. 1. What is the...
4
13331
by: Sanjay Dahiya | last post by:
I tried POSTing from XMLHttpRequest, i can get the XML right on server but responseXML from server is coming null. I can see the XML right in responseText. but responseXML is null. responseText to...
1
2132
by: Jordan | last post by:
Hi, I've declared the statement below in my form and somehow crystalReportViewer1 is always null. Is there anything wrong with the statement? private...
3
1850
by: Fett | last post by:
Problem: My member variables in the child class are always null. I don't know why this happens! I have a base class for handling a user flow in my web, CWebUseCase, the class is declared like:...
0
1419
by: Rob | last post by:
Here's my problem: I'm validating a XML file that I generate vs a static schema file, and am trying to get all the validation errors into a format that can be understood by a non-technical user. So...
1
3689
by: tomb | last post by:
My CustomCollection has several events based on ListChanged collection management, when the ListChanged event is raised the ListChangedEventArgs.PropertyDescriptor property is always null. Any...
4
2371
by: fishek | last post by:
I have a Master Page with a property defined in codebehind like this: private string profile; public string Profile { get { return profile; } } I can set the property value in the master...
3
3347
by: Judy K | last post by:
Greetings, Trying to assign a javabean property and get it in another page with <jsp:getProperty .... I can get it in the same page, but in different page it is always null. scope =...
0
1383
by: dba | last post by:
I'm having a problem with LINQ, I know my property isvalid is set to true, when I step through the code it's fine, this._IsValid = value is set to true, but when it gets to SendPropertyChanged it's...
0
7213
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7298
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4698
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1526
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.