问题
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