Reading json data returns in jQuery

I use the following code to get json formatted data:

$.ajax({
            type: "GET",
            url: "MyService.svc/GetSomeData",
            dataType: "text",
            success: function (data, textStatus) {

                alert("Test: " + data.toString());
            },
            error: function (xhr, textStatus, errorThrown) {
                alert("Error: " + (errorThrown ? errorThrown : xhr.status));
            }
        });

Data successfully returns to this call, and it looks like this:

{"d":"test data"}

My guess was that I could access the data as follows:

var myData = data["d"];

However, this always returns "undefined". What am I missing to get one row of “test data” data?

+3
source share
3 answers

What happens if you try data.d?

+5
source

Change dataType: "text",todataType: "json",

, , , , JSON, . d. , JSON javascript, jQuery , , JSON.

+8

I think both Sean and Thiago are correct: use {dataType: "json"}(in your variants $ .ajax ()) and access the value with data.d.

0
source

Source: https://habr.com/ru/post/1758468/


All Articles