2

I made this webservice that handles my database functions, and this is an AJAX call to one of the methods.

 $.ajax({
        type: "POST",
        url: "Service/dataBaseService.asmx/getRMAData",
        data: '{"RMAId": 1 }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: true,
        cache: false,
        success: function (data) {
            console.log(data);
            alert(data.RMA_ID);
        }
  });

this is what is logged:

({d:"[{\"RMA_ID\":1,\"RequestDate\":\"2013-02-28T00:00:00\",\"Company\":1,\"Status\":\"Accepted            \",\"Serial\":201764,\"LastChangeDate\":\"2013-02-28T00:00:00\",\"LastChangeBy\":\"Foreign    \",\"Price\":null}]"})

However alert(data.RMA_ID) returns undefined aswell as data.d.RMA_ID?

How can I get hold of the values?

1
  • Are the (...) really part of the response? Looks like broken JSONP. Commented Apr 24, 2013 at 11:53

2 Answers 2

6

The value of data that you've logged is an object with a property named d, that contains a string value. You could probably make adjustments at your server side to make the value of d an object rather than a string, but the way it is currently constructed, you would be able to parse it into an object using JSON.parse.

Once you've done that, the resulting object should be an array, containing one single object. Thus, your access to RMA_ID would be as follows:

var data = JSON.parse(data.d);
alert(data[0].RMA_ID);
Sign up to request clarification or add additional context in comments.

2 Comments

+1 deleting my answer because I had not initially realized that the value of d is a string, rather than an already-parsed object.
Thank you David, i am fairly new at this. I took a look at my serverside and made it return objects instead.
0

Using simple javascript you need to parse JSON response

var resp = eval('(' + data + ')');

or thru jQuery

var resp = jQuery.parseJSON(data);

now you can access the data using '.' and key name

console.log(resp.d[0].RMA_ID)

1 Comment

eval is disencouraged, JSON.parse is not really jQuery and no, resp.RMA_ID won't be defined either way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.