![](/img/trans.png)
[英]I'm not able to store the data to mysql database using HTML and JavaScript
[英]I'm not able to POST data using JavaScript Ajax
我能够使用 Ajax GET 请求从填充文本 web api 获取数据,但是当我使用 Post 请求时,我看不到任何返回到控制台的内容,也没有错误?
const xhr = new XMLHttpRequest();
const url = 'http://www.filltext.com/?rows=1&pretty=true&id={index}&fname={sahil}&lname={keshav}&company={business}';
let test = JSON.stringify({
id:124,
fname:'sunil',
lname:'singh',
company:'reebok'
});
function getData(){
xhr.onreadystatechange = () => {
if(this.readyState === 4 && this.status === 200){
let tes = JSON.parse(this.responseText)
console.log(tes);
}
}
xhr.open('POST',url,true);
xhr.setRequestHeader('Content-type','application/json');
xhr.send(test);
}
确保控制台记录所有有助于调试请求的信息,并添加错误处理 function。
此外,开发者控制台是您的朋友。 检查浏览器的控制台和网络选项卡 (Chrome) 以了解发生了什么。 确保选中网络选项卡中的 XHR 选项以仅显示 XMLHttpRequest 网络请求。
这是一个StackOverflow 帖子,可以帮助您朝这个方向发展。
const xhr = new XMLHttpRequest();
const url = 'http://www.filltext.com/?rows=1&pretty=true&id={index}&fname={sahil}&lname={keshav}&company={business}';
let test = JSON.stringify({
id:124,
fname:'sunil',
lname:'singh',
company:'reebok'
});
function getData(){
xhr.onreadystatechange = () => {
console.log( "onReadyStateChange:", xhr.readyState, xhr.status, xhr.statusText);
if(this.readyState === 4 && this.status === 200){
let tes = JSON.parse(this.responseText)
console.log(tes);
}
}
xhr.open('POST',url,true);
xhr.setRequestHeader('Content-type','application/json');
xhr.send(test);
// Add error handling:
xhr.onerror = function(){
console.log( "onError:", xhr.status, xhr.statusText);
// Triggers when request couldn't be completed
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.