我使用jQuery函数将数据传递给服务器。
data : {"id" : "3"}
如何从对象中获取3
值?假设我可以访问data
并输出{"id" : "3"}
我尝试了2种方法,但都没有效果。
方法1:
我尝试使用以下方法获取值:
$(this)[0].data.id
结果: undefined
方法2:
JSON.parse($(this)[0].data).id
结果: Unexpected token
打印到控制台如下:
[Object]
0
:
Object
accepts
:
Object
async
:
true
contentType
:
"application/json; charset=utf-8"
contents
:
Object
converters
:
Object
crossDomain
:
false
data
:
"{ 'id': '3' }"
dataType
:
"json"
dataTypes
:
Array[2]
error
:
(uploadifyDiv, response, a, b)
flatOptions
:
Object
global
:
true
hasContent
:
true
isLocal
:
false
jsonp
:
"callback"
jsonpCallback
:
()
processData
:
true
responseFields
:
Object
success
:
(response)
type
:
"POST"
url
:
"/TestApp/Pages/Uploadify.aspx/FileUploaded"
xhr
:
ci()
__proto__
:
Object
length
:
1
__proto__
:
Object[0]
答案 0 :(得分:1)
您可以data.id
访问它。
console.log(data.id);
如果是字符串化,则需要先解析它:
data = JSON.parse(data);
console.log(data.id);
或者@Alon评论过,它可能是另一个对象中的对象。在这种情况下,请执行
console.log(parentObj.data.id);