Javascript 在ajax post中将字符串转换为json对象
我有以下资料:Javascript 在ajax post中将字符串转换为json对象,javascript,Javascript,我有以下资料: SignUp.prototype.submitForm = function() { let that = this; $(document).on('click', this.buttonSubmitSelector, function(e) { e.preventDefault(); let current = $(this).closest('form'), data = current.seria
SignUp.prototype.submitForm = function() {
let that = this;
$(document).on('click', this.buttonSubmitSelector, function(e) {
e.preventDefault();
let current = $(this).closest('form'),
data = current.serialize();
console.log(JSON.parse(data));
// if(that.isEmailValid(emailVal)) {
// that.removeError(emailInput);
// $.ajax({
// type: "POST",
// url: current.attr('action'),
// data: data,
// success: function(){
// that.displaySuccess(current);
// that.resetForm(current);
// }
// });
// }
// else {
// that.showError(emailInput);
// }
});
}
但是,我得到以下错误:
VM26559:1 Uncaught SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)
at HTMLInputElement.<anonymous> (signUp.js:60)
at HTMLDocument.dispatch (jquery.js:5206)
at HTMLDocument.elemData.handle (jquery.js:5014)
VM26559:1未捕获的语法错误:JSON中位置1处的意外标记o
在JSON.parse()处
在HTMLInputElement。(signUp.js:60)
在HTMLDocument.dispatch(jquery.js:5206)
位于HTMLDocument.elemData.handle(jquery.js:5014)
数据为“form_build_id=form-wKUSwxF8He0krHpGo4N_vzyT9CuUD2HCWNF0dp3tpvk&form_id=sign_alert_form&nid=12&title=Production%20Training%20Scheme&email=as web%40hotmail.com”
我以前从未遇到过这个错误。怎么能修好呢
serialize方法将表单中的数据转换为URL编码的字符串
它不会将其转换为JSON,因此无法使用JSON解析器对其进行解析
您可以使用API对其进行解码(API是新的、有光泽的,在浏览器中工作可能需要一个API)
serialize()不输出JSON格式。。。
如果您使用的是jQuery:
data = JSON.stringify(current.serializeArray());
请控制台。记录(数据)并显示结果重复问题。
data = current.serialize();
var searchParams = new URLSearchParams(data);
for (let p of searchParams) {
console.log(p);
}
data = JSON.stringify(current.serializeArray());