0

I'm developing a form in which user must insert an username. I want to check on blur that username of user is valid:

I added this script:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>

IN HTML:

<input name="username" type="text" onblur="checkUsername()">

Script:

function checkUsername(){
    var usn = document.getElementsByName('username')[0];
    if(usn.value != "") {
       var html = $.ajax({
       type: "GET",
       url: "checkUsername.php?",
       data: "usr=" +usr.value 
       async: false,
       dataType: "text"}).responseText;   
       if(html == "si") {
          usn.style.backgroundColor = "green";
       } else {
          usn.style.backgroundColor = "red";
          usn.value = "Username still exists!";
       }
    }
}

So onBlur doesn't work, and when I submit form it appear an error like this:

HTTP Error 405.0 - Method Not Allowed
The page you are looking for cannot be displayed because an invalid method (HTTP verb) is being used.

What can I do? Where is the problem?

khr055
  • 26,892
  • 16
  • 34
  • 47
JackTurky
  • 12,431
  • 41
  • 126
  • 208

4 Answers4

2

First of all consider upgrading your JQuery to 1.6.x version.

Try this modified version of your script:

function checkUsername(){
    var usn = document.getElementsByName('username')[0];
    if(usn.value != "") {
       var html = $.ajax({
       url: "checkUsername.php",
       data: "usr=" +usr.value 
       async: false,
       dataType: "text"}).responseText;   
       if(html == "si") {
          usn.style.backgroundColor = "green";
       } else {
          usn.style.backgroundColor = "red";
          usn.value = "Username still exists!";
       }
    }
}
Salaros
  • 1,394
  • 1
  • 12
  • 33
  • 1
    Why `1.6.x`? The latest stable release is `1.7.1` – Cheery Jan 31 '12 at 23:18
  • I know, but sometimes an upgrade from an old version to the latest one breaks the existing code. While 1.6.x branch is not far away from 1.5.x it is full of bug fixes for $.ajax() function: http://blog.jquery.com/2011/05/03/jquery-16-released/ – Salaros Jan 31 '12 at 23:22
  • @JackTurky Are you sure that the request you are trying to do is not a cross-domain or userid/password protected one? Try to test php script separately, debug GET parameter, then do the same thing in javascript, by using console rather than alerts. – Salaros Jan 31 '12 at 23:27
  • done! i don't know where was the problem, i reindent all and update all } ;) – JackTurky Jan 31 '12 at 23:38
1

Usually when I run an AJAX command from Jquery, my data attribute is an array like such:

 $.ajax({
    type: "GET",
    url: "checkUsername.php",
    data: {
        usr: usr.value 
    },
   async: false,
   dataType: "text"}).responseText;   
StingeyB
  • 495
  • 3
  • 15
0

in the option add

dataType: 'jsonp',
ewwink
  • 15,852
  • 2
  • 35
  • 50
0

The other thing to do is check for javascript errors above your ajax call, which will also cause the 405 error...