HTTP Error 405.0 - Method Not Allowed using Jquery ajax get

白昼怎懂夜的黑 提交于 2019-12-25 01:20:01

问题


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?


回答1:


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!";
       }
    }
}



回答2:


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;   



回答3:


in the option add

dataType: 'jsonp',



回答4:


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



来源:https://stackoverflow.com/questions/9088352/http-error-405-0-method-not-allowed-using-jquery-ajax-get

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!