HTTP Error 405.0 - Method Not Allowed using Jquery ajax get
Asked Answered
D

4

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?

Dashboard answered 31/1, 2012 at 23:2 Comment(3)
https://mcmap.net/q/153088/-what-causes-an-http-405-quot-invalid-method-http-verb-quot-error-when-posting-a-form-to-php-on-iis/1164491 https://mcmap.net/q/153147/-server-error-405-http-verb-used-to-access-this-page-is-not-allowed/1164491 and read this one forums.asp.net/t/1650010.aspx/1Dilapidation
i'm using an hosting online, i can edit anythingDashboard
Do you have access to checkUsername.php ? check for redirects mentioned by the links above.Dilapidation
F
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!";
       }
    }
}
Fechter answered 31/1, 2012 at 23:14 Comment(4)
Why 1.6.x? The latest stable release is 1.7.1Dilapidation
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: blog.jquery.com/2011/05/03/jquery-16-releasedFechter
@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.Fechter
done! i don't know where was the problem, i reindent all and update all } ;)Dashboard
R
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;   
Razo answered 31/1, 2012 at 23:10 Comment(1)
@JackTurky it is not related to you jQuery script.Dilapidation
R
0

in the option add

dataType: 'jsonp',
Resupinate answered 11/11, 2012 at 4:32 Comment(0)
R
0

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

Rueful answered 5/11, 2013 at 1:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.