jquery - easyui 从 ajax 返回 undefined
问题描述
我提交表单,当我签入控制台时,它返回 JSON 数据。但我不能在 JSON 数据中使用对象。
$('#ff').form({
type: 'POST',
cache: false,
url: base_url + 'Api/lapor/submit',
dataType: 'json',
iframe: false,
onSubmit: function(param) {
param.id_item = sessionStorage.id_item;
param.token = sessionStorage.token;
},
onProgress: function(percent) {
$.messager.progress();
},
success: function(msg){
console.log(msg.status); //it will return undefined
$.messager.progress('close');
},
onLoadError: function(){
$.messager.alert('Error', 'Gagal', 'error');
}
});
我尝试console.log(msg)
并返回 JSON 数据
控制台
解决方案
当您使用 记录返回消息console.log(msg.status);
时,如果它写入结果,{message: "Data 1 telah disimpan", status: true}
那么您可以使用msg.status
或调用它msg['status']
但在您的情况下,它记录为{"message": "Data 1 telah disimpan", "status": true}
,因此您应该尝试使用"
包含的 likemsg['"status"']
或msg["\"status\""]
var msg1 = {"message": "Data 1 telah disimpan", "status": true}
var msg2 = {'"message"': "Data 2 telah disimpan", '"status"': true}
function checkdata1() {
console.log(msg1);
alert(msg1.message);
}
function checkdata2() {
console.log(msg2);
alert(msg2['"message"'])
}
<btn onclick="checkdata1()">Check Data 1</btn>
<br/>
<btn onclick="checkdata2()">Check Data 2</btn>
更新msg1
以下是使用记录时和msg2
记录时的区别console.log
Update 2 Because the return type of jeasyui
$.form
is a string
, so you need to parse it first to a JSON / javascript object by using JSON.parse
or the one found in the jeasyui
docs section Handle submit response here
推荐阅读
- git - 如何为 Github 中的表和函数上传 Kusto DB 架构?
- rust - Rust 打印后从控制台读取输入
- ios - AudioKit 5 幅度包络没有声音
- pbx - 为什么 fusionPBX 通话录音入站是 MONO,出站是 STEREO?
- sql - 如何使用循环获取一一列名是SQL
- r - 如何根据使用花括号 {} 的条件将数据框与另一个数据框连接起来
- javascript - 为什么我不能以这种方式删除用户对象?
- python - “AssertionError:如果没有 > 定义填充令牌,则无法处理批量大小 > 1”和 pad_token = eos_token
- c - 函数返回值优化?
- javascript - React 和 redux 工具包:如何添加从拖放事件中获取数据的 reducer