我是 Json/jquery/javascript 新手,只是坚持解决这个问题。 我有一个 Ajax PUT 调用,它将一些数据发送到服务器并返回不同的响应 json。 现在的问题是我无法访问 resposne 的属性。 请不要标记“重复”,因为我已经应用了我在 stackoverflow 上找到的任何内容,例如设置数据类型和内容类型。 我也尝试过 json.parse 但是该语句被忽略,我认为这是因为它已经是 Json 了。
request = $.ajax({
url: reporturl,
type: "PUT",
data: JSON.stringify(request),
dataType: "json",
beforeSend: function(xhr) {
xhr.setRequestHeader( "Content-type", "application/json" );
}
});
request.done(function (response, textStatus, jqXHR){
alert("Wow, it worked!");
alert(response); //returns [object object] seems ok
if (jqXHR.responseJSON) {
alert("is json"); //returns yes, seems ok
}
var fileJSON = response.Criteria;
alert(fileJSON); //returns undefined
现在:
1) jqXHR.responseJSON 返回 TRUE。
2) 根据 json 验证工具,“响应”是有效的 json。
响应如下所示:(需要将 Criteria 等元素加倍,我认为不会导致此问题。)
{
"response": {
"Criteria": {
"Criteria": [
{
"name": "UserRepCode1",
"value": "XX"
},
{
"name": "UserRepCode2",
"value": "YY"
}
]
},
"Files": {
"Files": [
{
"fSeq": 0,
"filetype": "spooler",
"filetext": "XXX X3.07 - 22/12/2014 17:03 mk \r\n\r\nUser defined report : \r\nClient From : \r\nClient To : \r\nEstate From : \r\nEstate To : \r\nManager From : \r\nManager To : \r\nProperty From : DLO REVOD Road DLO REVOD Road, DLO REVOD Road999, DLO REVOD Road2\r\n : Aberdeen, Grampian, SE10 7TZDLO REVOD Road DLO REVOD Road\r\n : DLO REVOD Road999, DLO REVOD Road2, Aberdeen, Grampian\r\nCurrency Type From : Pounds SterlingPounds Sterling\r\nCurrency Type To : Pounds SterlingPounds Sterling\r\nS/C Period : 01/01/2012 - 31/12/2012 Unreconciled-00101/01/2012 - 31/12/2012 Unreconciled-001\r\nBudget Detail : Expense ClassExpense Class\r\nSubTotal By Schedule : Yes \r\nSubTotal By Expense Class : No \r\nSubTotal By Expense Category : No \r\nNominal/Gross : Nominal Nominal\r\nDisplay Transaction : No \r\nActual Expenditure : Yes \r\nPre-Payments : Yes \r\nAccruals : Yes \r\nOverhead Recovery : Yes \r\nConvert Currency : No \r\nInc Property Budgets : No \r\n\r\nOutput To : Spool\r\nHeader : Yes\r\nOrientation/Font : Portrait, Selected\r\n \r\nMKDEBUG10 Email To : \r\nDLO X3.07 - 22/12/2014 17:03 mk Page: 0\r\n__________________________________________________________________________________________________________________________________\r\n\r\nClient : GROSV001 LTD(TA)\r\nEstate : 000000000338 Park, een\r\nPeriod : 001 01/01/2012 - 31/12/2012 Unrec\r\n\r\n Nomture BudDiff\r\n To Date\r\n__________________________________________________________________________________________________________________________________\r\n\r\n\r\nProperty: 000100 DLO REVOD \r\nSchedule: 01 rty \r\n----------------------------------------------------\r\nDLO X3.07 - 22/12/2014 17:03 mk Page: 0\r\n__________________________________________________________________________________________________________________________________\r\n\r\nClient : DLO\r\nEstate : 000000000338 DLO, PSO\r\nPeriod : 001 01/01/2012 - 31/12/2012 Unreconciled\r\n\r\n XLRO\r\n To Date For Period To Date To Date To Date\r\n__________________________________________________________________________________________________________________________________\r\n\r\nDLO X3.07 - 22/12/2014 17:03 mk Page: 0\r\n__________________________________________________________________________________________________________________________________\r\n\r\nClient : DLO\r\nEstate : 000000000338 DLO, PSO\r\nPeriod : 001 01/01/2012 - 31/12/2012 Unreconciled\r\n\r\n XLRO\r\n To Date For Period To Date To Date To Date\r\n__________________________________________________________________________________________________________________________________\r\n\r\nDLO X3.07 - 22/12/2014 17:03 mk Page: 0\r\n__________________________________________________________________________________________________________________________________\r\n\r\nClient : DLO\r\nEstate : 000000000338 DLO, PSO\r\nPeriod : 001 01/01/2012 - 31/12/2012 Unreconciled\r\n\r\n XLRO\r\n To Date For Period To Date To Date To Date\r\n__________________________________________________________________________________________________________________________________\r\n\r\nDLO X3.07 - 22/12/2014 17:03 mk Page: 0\r\n__________________________________________________________________________________________________________________________________\r\n\r\nClient : DLO\r\nEstate : 000000000338 DLO, PSO\r\nPeriod : 001 01/01/2012 - 31/12/2012 Unreconciled\r\n\r\n XLRO\r\n To Date For Period To Date To Date To Date\r\n__________________________________________________________________________________________________________________________________\r\n\r\nDLO X3.07 - 22/12/2014 17:03 mk Page: 0\r\n__________________________________________________________________________________________________________________________________\r\n\r\nClient : DLO\r\nEstate : 000000000338 DLO, PSO\r\nPeriod : 001 01/01/2012 - 31/12/2012 Unreconciled\r\n\r\n XLRO\r\n To Date For Period To Date To Date To Date\r\n__________________________________________________________________________________________________________________________________\r\n\r\n10 Expense Class 10 0.00 720.00 720.00 -720.00 -100.00%\r\n --------------- --------------- --------------- --------------- ---------\r\nTotal Schedule: 01 Property 0.00 720.00 720.00 -720.00 -100.00%\r\n --------------- --------------- --------------- --------------- ---------\r\n =============== =============== =============== =============== =========\r\nTotal Period/Prop: 001 0.00 720.00 720.00 -720.00 -100.00%\r\n =============== =============== =============== =============== =========\r\n =============== =============== =============== =============== =========\r\nGrand Total for Currency GBP 0.00 720.00 720.00 -720.00 -100.00%\r\n =============== =============== =============== =============== =========\r\n"
}
]
},
"UserMessages": {
"UserMessages": []
}
}
}
现在我陷入了困境,我应该如何检索单个属性(例如条件、文件和消息)以进行进一步处理。 请帮忙。
最佳答案
response
是保存对象的变量,而不是对象中的第一个键。
您需要引用第一个键
var fileJSON = response.response.Criteria;
关于javascript - Ajax Json 属性未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27610080/