JQuery/AJAX从外部文件读取数据

时间:2022-10-09 11:18:42

In HTML, there is a textarea. When textarea content is edited, I need the AJAX to read an external file and alert its content. I have this code which doesn't alert anything:

在HTML中,有一个textarea。当编辑textarea内容时,我需要AJAX读取外部文件并通知其内容。我有一个代码,它没有警告任何东西:

<!DOCTYPE html>

<textarea id="area">Sample Text</textarea>

   <!-- Include jquery -->
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>

<script type="text/javascript">

    $("#area").on("change keyup paste", function() {
        $.ajax({
          method: "POST",
          url: "externalFile.php",
          data: { content: $("#area").val() }
        })
          .done(function( msg ) {
            alert( "Reading Data: " + msg );
          });

    });

</script>

However, I know that the code successfully enters the jquery function because the following test alert works when I start typing in the textarea:

但是,我知道代码成功地进入了jquery函数,因为当我开始在textarea中输入时,下面的测试警报会工作:

<textarea id="area">Sample Text</textarea>

   <!-- Include jquery -->
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>

<script type="text/javascript">

    $("#area").on("change keyup paste", function() {
        alert("Test Alert");
    });

</script>

The external file is in the same directory - externalFile.php

外部文件位于同一个目录——externalFile.php中

This text is in external file!

Am I doing something wrong in the AJAX part?

我在AJAX部分做错了什么吗?

1 个解决方案

#1


3  

jQuery "slim" edition doesn't include the Ajax functionality. For this to work you need to use the full library. Simply replace

jQuery“slim”版本不包括Ajax功能。要使其工作,您需要使用完整的库。简单的替换

<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>

with

<script src="http://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>

#1


3  

jQuery "slim" edition doesn't include the Ajax functionality. For this to work you need to use the full library. Simply replace

jQuery“slim”版本不包括Ajax功能。要使其工作,您需要使用完整的库。简单的替换

<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>

with

<script src="http://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>