3

I have a web service which adds an item to the database after being called using JQuery Ajax. The web service returns a string, and I can't manage to pick up only the string part returned. Instead I receive {"d":"The message I want to display"} using alert(data);.

I also tried alert(Object.keys(JSON.parse(data))[0]); which returns d and alert(Object.keys(JSON.parse(data))[1]); or alert(data.d); returns Undefined.Here's what my code looks like

function AddAjaxJQuery() {
            var isbn = $('#<%= txtIsbn.ClientID %>').val();

            var pdata = { "book": { "Isbn": isbn} };

            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "/BookWebService.asmx/InsertBook",
                data: JSON.stringify(pdata),
                dataType: 'text',
                async: true,
                success: function (data, textStatus) {
                    alert(data);
                },
                error: function (error) {
                    alert(data);
                }
            });
        }

2 Answers 2

4

If your data is a string, then you should parse it to JSON first:

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

1 Comment

Yes, it returns Undefined using alert(data.d);
0

You have to access property d of data (response) so Replace success callback with

success: function (data, textStatus) {
    alert(data.d);
},

Comments

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.