1

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:

<!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:

<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

This text is in external file!

Am I doing something wrong in the AJAX part?

Nirav Madariya
  • 1,470
  • 2
  • 24
  • 37
Eduard Avetisyan
  • 471
  • 1
  • 4
  • 20

1 Answers1

3

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

<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>
ADyson
  • 57,178
  • 14
  • 51
  • 63