Sharon
Sharon

Reputation: 3909

jQuery Ajax call returning '[object XMLDocument]'

I have an HTML page which I want to populate using Ajax. I've copied code from other pages, (which are all in PHP, and I'm not sure if that matters), and it's returning [object XMLDocument]. In the other pages (the PHP ones) I get whatever I printed out in the routine.

Here's what I have:

index.html -

<html> ... </html>
<script>
$(document).ready(function() {
 getSplashHelpVideos();
});
</script>

In the javascript file -

function getSplashHelpVideos() {
 $.ajax({ 
   url: "include/get_help_videos.php",
   type: "POST",
   success: function(data) {
    alert(data);
   }
 });
 return;
}

In get_help_videos.php (obviously this is just temporary code to try to figure out how this works) -

<?php
 session_start();
 echo 'OK';
 return;
?>

So I was expecting (and wanting) it to pop up an alert saying 'OK', which is what it would do in my other routines, but it pops up [object XMLDocument] instead.

Am I doing something wrong? Or is it best to live with it, and parse the XMLDocument?

Upvotes: 14

Views: 32800

Answers (5)

Rahul Soni
Rahul Soni

Reputation: 86

change your response type to html/text in get_help_videos.php file

Upvotes: 0

vedmtripathi
vedmtripathi

Reputation: 11

You just need to tell the datatype(which direct the browser you are expecting response in the mentioned format only,e.g.: "text" format) . In this case I tested this in firefox and mozilla.and it works.. :)

Check the Response in firefox/Mozilla - you can also verify the coming response after ajax request... follow the below steps-- press F12 in firefox/mozilla --> go to "Console" tab --> go to "Response" sub tab. :)

function GetEmployeeListWS_REST() {        
            jQuery.ajax({
            url: "http://localhost:8080/RESTDemo/rest/hello/helloXML",
            async: false,
            type: 'GET',
            contentType: "text/xml; charset=utf-8",                
            dataType: "text",
             crossDomain: true,
            //data: packet,
            error: function (xhr, textStatus, errorThrown) { alert(xhr + ' ' + textStatus + '' + errorThrown); },
            success: function (response, status, xmlData) {

                $("#EmployeeDetailsWs").text(response);                    
            }
        });

    } // ends : fun()

Upvotes: -1

Ankur Verma
Ankur Verma

Reputation: 5933

try to set the content type of response to text/html first then say echo "ok" like this:

header('Content-type: text/html');

I got the same problem here and it solved this way only, since when we does not specify the content type of the response every browser treats the response in different format as and so.

Upvotes: 0

heobay
heobay

Reputation: 71

You can try the code below. I've just tested it on Firefox 15.0.1 and it works well:

$.post("include/get_help_videos.php", function(data)
{
    alert(data);
}, "text");

Upvotes: 1

Rory McCrossan
Rory McCrossan

Reputation: 337550

You need to include the datatype parameter on you AJAX call to indicate that you are simply expecting a text response:

function getSplashHelpVideos() {
    $.ajax({ 
        url: "include/get_help_videos.php",
        type: "POST",
        dataType: "text",
        success: function(data) {
            alert(data);
        }
    });
    return;
}

Upvotes: 26

Related Questions