JavaScript fetch API 不起作用初学者问题
2019-12-14
146
我有一个简单的代码,我试图发送帖子,但我的代码出了点问题。帖子不起作用,在幕后,我有一个简单的后端,它 100% 正常工作。所以问题出在上面的代码上。 请帮帮我,也许范围出了问题?
Api = function() {
this.header = new Headers({
'Accept': 'application/json',
'Content-Type': 'application/json'
})
};
Api.prototype.buildUrl = function(id) {
return "http://localhost:3000/db/shop_name/" + id;
};
Api.prototype.post = function(id, data) {
const urlPost = api.buildUrl(id.value);
return fetch(urlPost, {
method: "post",
body: JSON.stringify(data),
headers: this.header
.then(res => res.json()).then(json => console.log(JSON.stringify(json)))
};
Api.prototype.get = function(id) {
//const urlGet = api.buildUrl(id);
return fetch(id, {
method: "GET",
})
.then(resp => {
alert(resp.json());
return resp.json();
})
};
Api.prototype.getAlll = function() {
return fetch(url, {
method: "GET"
})
.then(resp => {
alert(resp.json());
return resp.json()
})
};
Api.prototype.update = function(id, data) {
const url = api.buildUrl(id);
return fetch(url, {
method: "PUT",
body: JSON.stringify(data)
})
.then(resp => {
return resp.json()
.catch(error => {
let notFound = "The server can not find requested resource";
document.getElementById("stars").innerHTML = notFound + error.status;
})
})
};
Api.prototype.addProduct = function(id, data) {
return this.post(id, data);
};
Api.prototype.deleteProduct = function(id) {
return this.delete(id);
};
Api.prototype.updateProduct = function(id, data) {
return this.update(id, data);
};
Api.prototype.getProduct = function(id) {
return this.get(id);
};
Api.prototype.getAllProducts = function() {
return this.getAlll;
};
const Main = function() {
this.id = document.getElementById("id");
this.addCount = document.getElementById("count");
this.addName = document.getElementById("name");
this.addPrice = document.getElementById("price");
};
Main.prototype.add = function() {
// const ido = this.id.value;
const data = {
"price": this.addPrice.value,
"name": this.addName.value,
"count": this.addCount.value,
};
// let id = api.buildUrl(this.id.value);
api.addProduct(this.id, data);
};
Main.prototype.update = function() {
const data = {
"price": this.price,
"name": this.name,
"count": this.count,
};
api.updateProduct(id, data);
};
Main.prototype.delete = function() {
let id = api.buildUrl(this.id);
api.deleteProduct(id);
};
Main.prototype.get = function() {
let id = api.buildUrl(this.id.value);
api.getProduct(id);
};
Main.prototype.getAll = function() {
api.getAllProducts();
};
const api = new Api();
const main = new Main();
let addButton = document.getElementById('postBtn');
addButton.addEventListener('click', function() {
main.add();
});
/*
addButton.addEventListener("click",main.add.bind(main));
*/
let updateButton = document.getElementById("updateBtn");
updateButton.addEventListener("click", function() {
main.update();
});
let deleteButton = document.getElementById("deleteBtn");
deleteButton.addEventListener("click", function() {
main.delete();
});
let getButton = document.getElementById("getBtn");
getButton.addEventListener("click", function() {
main.get();
});
let getAllButton = document.getElementById("getAllBtn");
getAllButton.addEventListener("click", function() {
let tst = main.getAll();
console.log(tst);
});
1个回答
我创建了一个最小的工作示例,它仅处理您的
post
方法,我认为问题在于您为发布请求“构建” URL 的方式:
Api.prototype.post = function(id, data) {
// WRONG!
// const urlPost = api.buildUrl(id)
// CORRECT!
const urlPost = this.buildUrl(id)
return fetch(...)
}
看看这个与您的代码具有类似结构的示例:
const Api = function() {
this.headers = new Headers({
'Accept': 'application/json',
'Content-Type': 'application/json'
})
}
Api.prototype.buildUrl = function(id) {
return `https://jsonplaceholder.typicode.com/posts`
};
Api.prototype.post = function(id, data) {
const urlPost = this.buildUrl()
return fetch(urlPost, {
method: 'POST',
body: JSON.stringify({
title: 'foo',
body: 'bar',
userId: 1
}),
headers: this.headers,
})
.then(response => response.json())
};
function main() {
const API = new Api()
const submitBtn = document.getElementById(`submitBtn`)
submitBtn.addEventListener(`click`, function() {
API.post()
.then(result => {
console.log(`data was submitted`, Date.now())
console.log(`result`, result)
})
.catch(error => {
console.error(`handle your errors!`)
})
})
}
main()
<button id="submitBtn" type="button">Click to submit data</button>
希望这会有所帮助。
编辑
您必须在其他地方修复此问题,而不仅仅是
post
方法。
首先,您无法在
Api.prototype.post
方法内访问
api
,它超出了范围,因为您使用的是
const
,因此当您尝试在您的对象:
const Api = function() { ... }
Api.prototype.buildUrl = function() { ... }
Api.prototype.post = function(id, data) {
const urlPost = this.buildUrl()
// ...
}
const api = new Api()
api.post(...)
其次,
始终处理您的错误并确保您
捕获
失败的承诺
。您很可能无法调试此问题,因为您错过了一些可以为您提供有关正在发生的事情的线索的错误。
Api.prototype.post = function(id, data) {
const urlPost = this.buildUrl()
return fetch(urlPost, {...})
.then(...)
.catch(error => { console.log(`error`, error.message) }
}
第三,再次,您遇到了范围问题 -
api
在您的
Api
和
Main
实例的方法中不可用:
Api.prototype.post = function(...) {
api.buildUrl(...) // NOT IN SCOPE!
}
Main.prototype.getAll = function() {
api.getAllProducts() // ALSO NOT IN SCOPE
}
请改为执行以下操作:
const Main = function(api) {
this.api = api
// ...
}
Main.prototype.add = function() {
const data = {...}
return this.api.addProduct(this.id, data)
}
const api = new Api()
const main = new Main(api) // PASS `api` TO A CONSTRUCTOR
const btn = document.getElementById(`button`)
btn.addEventListener(`click`, function() {
main.add()
.then(...)
.catch(...)
})
返回并修复
Api
和
Main
内的所有方法!
这是一个有效示例:
const Api = function() {
this.headers = new Headers({
'Accept': 'application/json',
'Content-Type': 'application/json'
})
}
Api.prototype.buildUrl = function(id) {
return `https://jsonplaceholder.typicode.com/posts`
};
Api.prototype.post = function(id, data) {
const urlPost = this.buildUrl()
return fetch(urlPost, {
method: 'POST',
body: JSON.stringify({
title: 'foo',
body: 'bar',
userId: 1
}),
headers: this.headers,
})
.then(response => response.json())
};
Api.prototype.addProduct = function(id, data) {
return this.post(id, data)
}
function Main(api) {
this.api = api
this.id = 1
}
Main.prototype.add = function() {
const data = {
"price": 1,
"name": "Bob",
"count": 20
}
return this.api.addProduct(this.id, data)
}
const api = new Api()
const main = new Main(api)
const submitBtn = document.getElementById(`submitBtn`)
submitBtn.addEventListener(`click`, function() {
main.add()
.then(result => {
console.log(`data was submitted`, Date.now())
console.log(`result`, result)
})
.catch(error => {
console.log(`handle your errors!`)
})
})
<button id="submitBtn" type="button">Click me!</button>
goto
2019-12-14