1

I have a web page that is making an AJAX call which echoes a XML string in below format:

<ECG>Abnormal</ECG><SP>10.99</SP><BP>120/90</BP><OXY>139</OXY><TMP>23</TMP>

AJAX call

$.ajax({
        type:'post',
        url: 'check_status.php',
        dataType: 'xml',
        success: function(xml) {
                        var ecg = $(xml).find("ECG").text(); 
                        var sp = $(xml).find("SP").text(); 
                        var bp = $(xml).find("BP").text(); 
                        var oxy = $(xml).find("OXY").text(); 
                        var tmp = $(xml).find("TMP").text(); 
                        alert(tmp);
        },
        error: function(){
                        alert('Error');
                        update();
        }
        });

The XML response is simply created by PHP backend script by constructing the XML string:

$resp = "<ECG>" . $ecg . "</ECG>" ....
echo $resp;

But still the alert in the AJAX error method is called - is there something else that I need to do from backend script.

4
  • Write your complete php code. Commented Apr 8, 2014 at 8:11
  • 2
    The response isn't well formed XML. You're missing a document node which wraps the other nodes. Commented Apr 8, 2014 at 8:11
  • Thanks @hek2mgl for pointing out the issue - I am able to receive and parse the XML response now Commented Apr 8, 2014 at 8:22
  • You are welcome.. I'll post an answer ... Commented Apr 8, 2014 at 8:31

1 Answer 1

1

As I told in comments, the response isn't well formed XML. You're missing a document node which wraps the other nodes. Like this:

<?xml version="1.0"?>
<RESPONSE>
  <ECG>Abnormal</ECG>
  <SP>10.99</SP>
  <BP>120/90</BP>
  <OXY>139</OXY>
  <TMP>23</TMP>
</RESPONSE>

Also you are encouraged to set the proper content type header from PHP:

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

(before the output)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.