Ajax请求无法进去到Servlet程序问题解决方法!!!

代码如下:

我原来的代码是这样写的,进不去servlet程序

		<script type="text/javascript">
				$("#username").blur(function (){
					var username = this.value;
					$.getJSON("http://localhost:8080/book/userServlet", "action=ajaxExistsUsername&username=" +
							username, function (data) {
						if (data.existsUsername) {
							$("span.errorMsg").text("用户名已存在!");
						}if (username == "") {
							$("span.errorMsg").text("用户名不能为空!");
						} else {
							$("span.errorMsg").text("用户名可以使用!");
						}
					});
				});
  </script>

当把代码加到$(funtion(){ … })里,就成功了

$(function () {
				$("#username").blur(function (){
					var username = this.value;
					$.getJSON("http://localhost:8080/book/userServlet", "action=ajaxExistsUsername&username=" +
							username, function (data) {
						if (data.existsUsername) {
							$("span.errorMsg").text("用户名已存在!");
						}if (username == "") {
							$("span.errorMsg").text("用户名不能为空!");
						} else {
							$("span.errorMsg").text("用户名可以使用!");
						}
					});
				});
			});

解决!


版权声明:本文为qq_41210487原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。