Javascript jQuery Ajax 调用返回“[object XMLDocument]”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5810525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:00:05  来源:igfitidea点击:

jQuery Ajax call returning '[object XMLDocument]'

javascriptjqueryajax

提问by Sharon

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.

我有一个 HTML 页面,我想使用 Ajax 填充它。我已经从其他页面复制了代码(都在 PHP 中,我不确定这是否重要),并且它正在返回[object XMLDocument]. 在其他页面(PHP 页面)中,我得到了在例程中打印出来的任何内容。

Here's what I have:

这是我所拥有的:

index.html -

index.html -

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

In the javascript file -

在 javascript 文件中 -

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) -

在 get_help_videos.php (显然这只是试图弄清楚它是如何工作的临时代码) -

<?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.

所以我期待(并希望)它弹出一个警告,说“OK”,这是它在我的其他例程中会做的,但它却弹出了[object XMLDocument]

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

难道我做错了什么?还是最好忍受它,并解析XMLDocument?

回答by Rory McCrossan

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

您需要在 AJAX 调用中包含数据类型参数,以表明您只是在期待文本响应:

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

回答by heobay

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

你可以试试下面的代码。我刚刚在 Firefox 15.0.1 上测试过它,它运行良好:

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

回答by Ankur Verma

try to set the content type of responseto text/htmlfirst then say echo "ok"like this:

尝试首先将响应的内容类型设置为text/html然后像这样说echo "ok"

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

I got the same problem hereand 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.

我在这里遇到了同样的问题,它只能通过这种方式解决,因为当我们没有指定响应的内容类型时,每个浏览器都会以不同的格式处理响应。

回答by Rahul Soni

change your response type to html/textin get_help_videos.php file

将您的响应类型更改为html/textget_help_videos.php 文件

回答by vedmtripathi

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.. :)

你只需要告诉数据类型(它直接引导浏览器你只希望以提到的格式响应,例如:“文本”格式)。在这种情况下,我在 Firefox 和 mozilla 中对此进行了测试。它有效.. :)

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. :)

检查 firefox/Mozilla 中的响应 - 您也可以在 ajax 请求后验证即将到来的响应...按照以下步骤操作 - 在 firefox/mozilla 中按 F12 --> 转到“控制台”选项卡 --> 转到“响应”子选项卡。:)

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()