Imad Imad - 2 months ago 5
jQuery Question

Get the data value from an object using jQuery

I am passing data to a server using a jQuery function.

data : {"id" : "3"}


How can I get the
3
value from the object? Suppose I can access
data
and it outputs
{"id" : "3"}
I have tried 2 methods but neither works.

Method 1:

I tried to get the values using:

$(this)[0].data.id


Result:
undefined


Method 2:

JSON.parse($(this)[0].data).id


Result:
Unexpected token





Printing to the console looks like:

[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]

Answer

You can access it by data.id.

console.log(data.id);

If it is stringified, you need to parse it first:

data = JSON.parse(data);
console.log(data.id);

Or as @Alon has commented, it might be a object within another object. In this case, do

 console.log(parentObj.data.id);
Comments