Uncaught Typeerror: $.ajax is not a Function?

Asked

Viewed 3,575 times

1

What problem with my code, on the console shows this error:

home:85 Uncaught TypeError: $.ajax is not a function
        at HTMLFormElement.<anonymous> (home:85)
        at HTMLFormElement.dispatch (jquery-3.2.1.slim.min.js:3)
        at HTMLFormElement.q.handle (jquery-3.2.1.slim.min.js:3)

Researching I found this in Soen:

My code javascript

<script type="text/javascript">
          $(function() {
            $('#login-form').submit(function(e) {
              e.preventDefault(), $('#btn-login').html('Autenticando...');

              var username = $('#username').val();

              if (/^[a-zA-Z0-9_ ]*$/.test(username)) {
                $('.form-message').css('display', 'block'),
                $('.message').html('Existem caracteres especiais no seu usuário. Se estiver usando <strong>"@"</strong>, remova-o!');

                return false;
              }

              $.ajax({
                type: 'POST',
                data: $(this).serialize(),
                success: function(s) {
                  alert('ok');
                }
              });
            });
          });
        </script>
  • which version of jquery is using?

  • I got it, mate, thanks. The version is up there, jquery-3.2.1.slim.min.js

  • Actually it was not the intention but I managed to tidy up and put the answer.

1 answer

2


Boys, I found the mistake on this line:

if (/^[a-zA-Z0-9_ ]*$/.test(username)) 

I traded for

if (0 == /^[a-zA-Z0-9_ ]*$/.test(username)) 

Remembering the version slim of jquery does not have $.ajax({})

Reference

Sometimes you don’t need ajax, or you prefer to use one of the Many standalone Libraries that Focus on ajax requests. And often it is Simpler to use a Combination of CSS and class Manipulation for all your web animations. Along with the regular version of jQuery that includes the ajax and effects modules, we’ve Released a "slim" version that excludes These modules. The size of jQuery is very rarely a load performance Concern These days, but the slim build is about 6k gzipped bytes smaller than the regular version - 23.6k vs 30k. These files are also available in the npm package and on the CDN:

  • Good tip on jquery’s 'slim' doesn’t have $.ajax({}) !!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.