1

I have the following ajax call to make a call to webservice and retreive back the json data

$(document).ready(function () {
            $.ajax({
                type: 'POST',
                contentType: "application/json; charset=utf-8",
                url: "Test.asmx/GetImages",
                success: function (msg) {
                    //var data = JSON.parse(msg); 
                    alert(msg.d);
                   $.each(msg.results, function (i, tweet) {
                        alert(msg.d);
                        $("#imagelist").append('<p><img src="' + tweet + '" />' + tweet + '</p>');
                    });
                }
            });
 });

I have following json result obtained from the ajax call, I am finding difficult to append the img source .

{"d":"[\r\n  {\r\n    \"imagepath\": \"images/01.jpg\"\r\n  },\r\n  {\r\n    \"imagepath\": \"images/02.jpg\"\r\n  },\r\n  {\r\n    \"imagepath\": \"images/03.jpg\"\r\n  },\r\n  {\r\n    \"imagepath\": \"images/04.jpg\"\r\n  }\r\n]"}
1

1 Answer 1

5

Just specify dataType: 'json' and jquery will do that automatically for you

http://api.jquery.com/jQuery.ajax/

Sign up to request clarification or add additional context in comments.

11 Comments

Thanks for the reply , how to individually extract images/01.jpg,images/02.jpg................ , from the result data from $.each function
@mahesh: iterate over msg.d
Its behaving crazy , when i execute saying $.each({ "d": "[\r\n {\r\n \"imagepath\": \"images/01.jpg\"\r\n },\r\n {\r\n \"imagepath\": \"images/04.jpg\"\r\n },\r\n {\r\n \"imagepath\": \"images/04.jpg\"\r\n },\r\n {\r\n \"imagepath\": \"images/04.jpg\"\r\n }\r\n]" }, function (k, v) { alert("Value: " + v); });
@mahesh: it's triggered once because the object you passed has only one property with name d. And you need to pass array instead
@mahesh: no, traverse over msg.d, not over msg.d.imagepath (it is the 3rd time :-S)
|

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.