基于发布值的XMLHttpRequest响应状态
XMLHttpRequest response status based on posted value
我有一个JavaScript函数,它执行XMLHttpRequest。这是我的密码。
function addbilldetails() {
// Cancel the form submit
event.preventDefault();
// The URL to POST our data to
var postUrl = 'http://example.com/post.php';
// Set up an asynchronous AJAX POST request
var xhr = new XMLHttpRequest();
xhr.open('POST', postUrl, true);
// Prepare the data to be POSTed
var clientId = "clientid",
submittype = "a",
name = encodeURIComponent(document.getElementById('name').value),
billno = encodeURIComponent(document.getElementById('billno').value),
mobileno = encodeURIComponent(document.getElementById('mobileno').value),
phoneno = encodeURIComponent(document.getElementById('phoneno').value),
netAmount = encodeURIComponent(document.getElementById('netAmount').value);
var params = 'clientId=' + clientId +
'&billno=' + billno +
'&mobileno=' + mobileno +
'&phoneno=' + phoneno +
'&netAmount=' + netAmount +
'&type=' + submittype +
'&name=' + name;
// Replace any instances of the URLEncoded space char with +
params = params.replace(/%20/g, '+');
// Set correct header for form data
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
// Handle request state change events
xhr.onreadystatechange = function() {
// If the request completed
if (xhr.readyState == 4) {
statusDisplay.innerHTML = '';
if (xhr.status == 200) {
// If it was a success, close the popup after a short delay
statusDisplay.innerHTML = 'Saved!';
document.getElementById('save').disabled = false;
// window.setTimeout(window.close, 1000);
} else {
// Show what went wrong
statusDisplay.innerHTML = 'Error saving: ' + xhr.statusText;
}
}
};
// Send the request and set status
xhr.send(params);
statusDisplay.innerHTML = 'Saving...';
document.getElementById('save').disabled = true;
}
现在,上面的代码工作得很好,并在POST
上返回200
。但我希望它根据发布的值在UI上返回自定义消息。
如果值POST
ed小于数据库中的值,我希望它给出"输入有效数字"或类似的内容。
我是XMLHttpRequest
的新手。我不知道如何做到这一点。如有任何帮助,我们将不胜感激。
您是否考虑过statusDisplay.innerHTML = 'Saved!';
:
statusDisplay.innerHTML = xhr.responseText;
如果你这样做,那么你的状态显示将等于你的post.php
回声。
例如,在post.php 中
<?php
//handling $_POST['clientId'] ... etc
if (error)
echo "Enter Valid Number";
else
echo "Saved!";
相关文章:
- Nodejs服务器:加载资源失败:服务器的响应状态为404(未找到)
- 带Jquery的wep API,加载资源失败:服务器响应状态为404(未找到)
- 从服务器到客户端的响应状态
- 如何捕获页面上的所有请求和响应,并根据每个响应状态的需要执行操作
- AngularJS:捕获所有响应状态 500
- 错误:调用 waitForCondition 时错误响应状态:13
- React子组件不响应状态更改
- Ajax/JSON服务器的响应状态为415(不支持的媒体类型)
- 未能加载资源:服务器的响应状态为412(前提条件失败)
- 未能加载资源:服务器的响应状态为405(不允许使用方法)
- 基于发布值的XMLHttpRequest响应状态
- ReactJs:响应状态更改而更改状态
- Spring RequestMapping错误:服务器的响应状态为404(未找到)
- jQuery..load() 不会给出响应/状态
- Steam Post请求-加载资源失败:服务器的响应状态为400(错误请求)
- 加载资源失败:服务器使用Ajax响应状态为500
- ajax调用中没有响应状态,不确定原因
- 在Google可视化API中访问响应状态
- Ajax错误:加载资源失败:服务器响应状态为500(内部服务器错误)
- Ajax响应状态为200,但显示错误消息