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?

7
  • Look in the console in your browser at the network tab and see if it's calling externalFile.php and what response it's getting. Commented May 25, 2017 at 12:48
  • instead .done() in $.ajax(), use success: parameter. Commented May 25, 2017 at 12:49
  • 1
    @NiravMadariya, success is deprecated. Commented May 25, 2017 at 12:52
  • Inside the php file, you need to echo something in order to receive it in the AJAX function Commented May 25, 2017 at 12:52
  • 1
    @EduardAvetisyan: Ah, that's your problem then. jQuery slim doesn't include the ajax functions apparently. Commented May 25, 2017 at 12:55

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.