Strobe_
Strobe_

Reputation: 515

Add JSON object to DIV

I have a JSON object that I'm getting as a response to an AJAX call:

{ "Score": 5, "OS": "Windows 7" }

I want to add it to a div but the following does not work, data.OS or data.Score just return as undefined

$.ajax({
     type: "POST",
     url: '/details',
     data: JSON.stringify(IP), 
     contentType: 'application/json;charset=UTF-8',   
     success: function(data) {

        $('#OSdetails').append('<div id="details">Operating System: ' + data.OS + '</div>');

     }
}); 

What am I doing wrong?

Upvotes: 3

Views: 874

Answers (1)

iCollect.it Ltd
iCollect.it Ltd

Reputation: 93551

$.ajax({
     dataType: 'JSON',         <==== THIS IS MISSING
     type: "POST",
     url: '/details',
     data: JSON.stringify(IP), 
     contentType: 'application/json;charset=UTF-8',   
     success: function(data) {

dataType specifies the expected data type and allows for automated conversion

Upvotes: 7

Related Questions