0

i want to pass an object from c# to jquery but it didn't work. Here is my code c#:

public class ImgLink
{
    public string img;
}

[WebService(Namespace = "")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class ImgService : WebService
{
    List<ImgLink> Imgs = new List<ImgLink>{
        new ImgLink{img="/kazvan-1.jpg"},
        new ImgLink{img="/kazvan-2.jpg"},
        new ImgLink{img="/wojno-3.jpg"}
    };
    [WebMethod]
    public List<ImgLink> GetAllImgs()
    {
        return Imgs;
    }
}

Script:

function getImgs() {
     var myArray = [];
     $.ajax({
         type: "POST",
         url: "ImgService/GetAllImgs",
         data: "{}",
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         success: function (response) {
             var cars = response.d;
             $.each(cars, function (index, car) {
                 myArray.push({ "image": +car.img });
             });
             alert("success");
         },
         failure: function (msg) {
             alert("fail");
         }
    });
    return myArray;
}

my result: MyArray has nothing.

expected result:

myArray = [{ image: '/kazvan-1.jpg' },{ image: '/kazvan-2.jpg' },{ image: '/wojno-3.jpg' }]
5
  • 3
    You said your expected result, but what is your ACTUAL result? If the url Really ImgService or is it ImgService.asmx? Commented Sep 5, 2013 at 3:52
  • Your best bet is to return a JsonResult of your object from the function, then you will have the object available in Javascript. Commented Sep 5, 2013 at 3:58
  • 1
    Well, this line: myArray.push({ "image": +car.img }); is wrong. it should just be: myArray.push({ "image": car.img }); Does that help? I ran your code and it worked fine (except the +car.img part. Even then I still had an array with 3 items) Commented Sep 5, 2013 at 4:02
  • 1
    Look in Fiddler or Chrome developer tools Network tab or your favorite tool for network traffic and see what the response being returned is. You might also want to look at Dave Ward's article about ASP.NET's ".d", encosia.com/never-worry-about-asp-net-ajaxs-d-again. What I usually do is the following var cars = response.d || response Commented Sep 5, 2013 at 4:03
  • You are not responding to any of the answers or comments. please respond. -1 Commented Sep 5, 2013 at 5:53

2 Answers 2

1

Try this:

public class ImgLink
{
    public string img;
}


    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
     [System.Web.Script.Services.ScriptService]
public class ImgService : WebService
{
    List<ImgLink> Imgs = new List<ImgLink>{
        new ImgLink{img="/kazvan-1.jpg"},
        new ImgLink{img="/kazvan-2.jpg"},
        new ImgLink{img="/wojno-3.jpg"}
    };
    [WebMethod]
    public List<ImgLink> GetAllImgs()
    {
        return Imgs;
    }
}

and jQuery:

function getImgs() {
     var myArray = [];
     $.ajax({
         type: "POST",
         url: "ImgService/GetAllImgs",
         data: "{}",
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         success: function (response) {
             var cars = response.d;
             $.map(cars, function (item) {//use .map method
                 myArray.push({ "image": +item.img });
             });
             alert("success");
         },
         failure: function (msg) {
             alert("fail");
         }
    });
    return myArray;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need to specify in your service that you want to return JSON, like this:

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

Omitting that returns XML instead.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.