【发布时间】:2021-06-11 16:51:21
【问题描述】:
我需要知道为什么我的发布请求没有创建新的 JSON 文件。我正在使用 JS POST 获取请求。代码如下。
//Assume all the variables are defined
fetch(lowercaseUsernameStr+'_profile_data.json', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: {
"publicData": {
"username": usernameStr,
"userDescription": userDescription,
"favoriteGameData": {
"favoriteGameCategory": favoriteGameCategoryStr,
"favoriteGameNum": favoriteGameNum
},
"stats": {
"level": 1,
"levelXP": 0,
"maxLevelXP": 100,
"XPUntilNextLevel": 100,
"totalXP": 0,
"trophies": 0
}
},
"privateData": {
"password": passwordStr,
"email": email,
"telNum": telNum,
"userProfilePageVisibilityYesOrNo":
optionsSelectedOptionStr,
"userProfilePictureSrc": userProfilePictureSrc
}
}
})
.catch((error)=>{
alert("Sorry! We could not create your account! Please report the problem and wait until it's fixed! Here is the error: " + error);
//Assume the local url exists
document.location = 'contact_and_email_will.html';
});
【问题讨论】:
-
“创建一个新的 JSON 文件”是什么意思?
-
我了解到 post 请求可以创建新文件并向其中添加内容,而 put 请求只能编辑已经存在的文件。
-
@WillTechX20 您了解了一个约定,但这并不意味着任何 HTTP 服务器都会处理这样的 POST/PUT 请求。
标签: javascript post request fetch