AJAX 代码定义错误或不响应
ajax code define error or doesn't respond
我是ajax的新手。研究基础来自wrook的博客。
我使用以下代码使用 ajax 插入到我的数据库中。
但是代码除了"稍等一下"之外不会打印任何其他内容。
JsFiddle 链接Ajax代码如下:-
/* ---------------------------- */
/* XMLHTTPRequest Enable */
/* ---------------------------- */
function createObject() {
var request_type;
var browser = navigator.appName;
if (browser == "Microsoft Internet Explorer") {
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}
else {
request_type = new XMLHttpRequest();
}
return request_type;
}
var http = createObject();
function insertReply() {
if (http.readyState == 4) {
var response = http.responseText;
// else if login is ok show a message: "Site added+ site URL".
document.getElementById('insert_response').innerHTML = 'User added:' + response;
}
}
/* -------------------------- */
/* INSERT */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */
var nocache = 0;
function insert() {
// Optional: Show a waiting message in the layer with ID login_response
document.getElementById('insert_response').innerHTML = "Just a second...";
// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
var username = encodeURI(document.getElementById('Username').value);
var name = encodeURI(document.getElementById('Name').value);
var password = encodeURI(document.getElementById('Password').value);
document.getElementById('insert_response').innerHTML = 'h ' + username + ' ' + name;
// Set te random number to add to URL request
nocache = Math.random();
// Pass the login variables like URL variable
http.open('get', 'insert.php?name=' + name + '&username=' + username + '&password=' + password);
http.onreadystatechange = insertReply;
http.send(null);
}
插入 db 的 php 代码是 ::--
if(isset($_GET['name']) && isset($_GET['username'])&& isset($_GET['password'])){
$name= $_GET['name'];
$username= $_GET['username'];
$password= $_GET['password'];
$sql = "INSERT INTO `vidyasims`.`user_accounts` (`UserID`, `UserName`, `Name`, `Password`) VALUES (NULL, '. $username. ','. $name. ', '. $password. ');";
$insertSite= mysql_query($sql) or die(mysql_error());
//If is set URL variables and insert is ok, show the site name -->
echo $username;
} else {
echo 'Error! Please fill all fileds!';
欢迎支持。提前谢谢。
注意 - 上面的代码将新用户添加到数据库!
您缺少 html 表单输入的 id,并且无法使用 document.getElementById() 检索其值 快速修复:
<td><input type="text" id="Name" name='Name' value='' ><br></td>
<td><input type="text" id="Username" name='UserName' value=''></td>
<td><input type="password" id="Password" name='Password'></td>
为什么
你不这样使用jQuery Ajax,我认为这是你工作非常简单和容易的方法:
JQuery Code
<script language="javascript">
$(document).ready(function(){
$("#myHtmlButtonID").bind("click",function(){
$('#insert_response').html("Just a second...");
var username = $('#Username').val();
var name = $('#Name').val();
var password = $('#Password').val();
/* add some validation if needed here */
$.ajax({
url: "yourPHPpage.php",
data: {
name: name,
username:username,
password: password
},
type: "get",
success: function(txt){
$('#insert_response').html('User added:' + response);
},
error: function(xhr){
$('#insert_response').html('Error: - '+xhr.status+ ' '+ xhr.statusText);
}
});
});
});
</script>
相关文章:
- 基于类的单击事件在10行之后不响应-数据表
- "所请求的名称已经作为不同类型的“”存在;HTTP 409响应错误
- SyntaxError:意外的标记else(我在代码中找不到错误)
- 使用来自AngularJS的Web API JSON响应-错误:应为和数组,但得到了一个对象
- dhtmlx多个调度器不工作错误”;调度程序未定义”;
- 如何在php中添加按钮而不是错误状态和成功消息
- 精灵与身体错位,可以'在显示循环中找不到错误
- Json响应错误Json.parse:意外的数据结束错误
- AngularJs-$httpProvider.interceptors多重响应错误
- $.ajax在跨域url上不返回错误函数
- 可以'找不到错误
- 角度依赖项和提供程序不可用错误
- ASP.Net MVC 5 Ajax 请求在编辑页面上不存在错误__RequestVerificationToken
- 使用ajax的ReCaptcha响应错误
- JQuery 验证引擎不工作,不显示错误消息
- jQuery ajax 不返回错误或成功或总是返回
- 意外令牌 } 找不到错误
- AJAX GET 请求内容不响应 jQuery
- AJAX 代码定义错误或不响应
- 身份工具箱不工作-错误代码:IDP的错误响应