想知道javascript中是否有一个函数没有jquery或任何允许我序列化表单并访问序列化版本的框架?
我正在尝试从React / Redux / Django webapp删除jQuery,并用$.ajaxFetch API 替换该方法。我或多或少地使我的所有GET请求都能正常工作,并且似乎能够实现POST请求,但似乎无法以将POST数据实际放入Django request.POST对象的方式格式化请求。每次我点击/ sign_in视图时,该request.POST对象都是空的。我整个应用程序的后端都是使用Django表单构建的(没有Django模板,只有React控制的组件),我真的不想不必重写所有视图以使用request.body或request.data。
这是我认为可能相关的所有代码,请告诉我是否还有更多有用的代码:
这是咖喱函数,用于构建完整的POST数据并附加CSRF令牌:
const setUpCsrfToken = () => {
const csrftoken = Cookies.get('csrftoken')
return function post (url, options) {
const defaults = {
'method': 'POST',
'credentials': 'include',
'headers': {
'X-CSRFToken': csrftoken,
'Content-Type': 'application/x-www-form-urlencoded'
}
}
const merged = merge(options, defaults)
return fetch(url, merged)
}
}
export const post = setUpCsrfToken()
Run Code Online (Sandbox Code Playgroud)
这是我在React应用程序中使用的API方法:
export const signIn = data => {
return post('/api/account/sign_in/', data)
} …Run Code Online (Sandbox Code Playgroud) 我正在使用基本的访存来从快速服务器获取数据,该服务器从数据库查询数据。所以我想做一个登录系统,我想通过获取请求从输入字段中发送用户名/密码。因此我可以执行逻辑检查,以查看密码是否与服务器中的数据匹配。并回应结果。是否可以进行传递参数的提取?
以下更新的代码:
let fetchData = {
method: "post",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: this.state.user,
password: this.state.password
})
}
var fromServer = fetch('http://localhost:3000/', fetchData)
.then(function(response){
if( !response.ok){
throw Error (response.statusText);
}
return response.json();
})
.then(function(response) {
console.log(response);
})
.catch(error => console.log("there was an error --> " + error));
Run Code Online (Sandbox Code Playgroud)
快速功能
app.post('/', function(req, res){
var uname = req.body.username;
var pw = req.body.password;
console.log(uname);
console.log(pw);
});
Run Code Online (Sandbox Code Playgroud)