jQuery Ajax调用返回'[object XMLDocument]'

Sha*_*ron 15 javascript ajax jquery

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

这就是我所拥有的:

index.html -

<html> ... </html>
<script>
$(document).ready(function() {
 getSplashHelpVideos();
});
</script>
Run Code Online (Sandbox Code Playgroud)

在javascript文件中 -

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

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

<?php
 session_start();
 echo 'OK';
 return;
?>
Run Code Online (Sandbox Code Playgroud)

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

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

Ror*_*san 25

您需要在AJAX调用中包含datatype参数,以指示您只是期望文本响应:

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