0

This is my WebService Method:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

public string getlastimages()
{
    DBservices DBS = new DBservices();
    List<string> ImageUrls = new List<string>();
    try
    {
       DBS.getLastImages();
       ImageUrls = DBS.ImgUrlList;
    }
    catch(Exception ex)
    {
        throw ex;
    }

    JavaScriptSerializer js = new JavaScriptSerializer();
    string jsonImage = js.Serialize(ImageUrls);
    return jsonImage;
}

This is the data that i'm receiving after running this method:

<string xmlns="http://tempuri.org/">
["~\\images\\benny\\1149468-18.jpg","~\\images\\lior\\photo4.jpg","~\\images\\oren\\photo3.jpg","~\\images\\oren\\photo2.jpg","~\\images\\oren\\photo1.jpg"]
</string>

I'm new with $.ajax method, and I need help writing the $.ajax method to retrieve this data from that list. Any help would be great.

1 Answer 1

1

You no need to convert the List<string> to json formatted string. It will be handled internally by using JavaScriptSerializer, since you already decorated with [ScriptMethod(ResponseFormat = ResponseFormat.Json)]. Check MSDN

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script
[ScriptService]
public class Service: System.Web.Services.WebService 
{
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]    
    public List<string> getlastimages()
    {
      DBservices DBS = new DBservices();
      List<string> ImageUrls = new List<string>();
      try
      {
       DBS.getLastImages();
       ImageUrls = DBS.ImgUrlList;
      }
      catch(Exception ex)
      {
        throw ex;
      }
      return ImageUrls;
    }
}

jQuery

$.ajax({

url:"service.asmx/getlastimages",
type:"POST",
dataType:'json',
contentType: "application/json; charset=utf-8",
success:function(data){
   var result;
   if (data.hasOwnProperty('d')) {
    result=data.d;// ASP.Net framework will add d
   }
   else
   {
    result=data;
   }
}
});
Sign up to request clarification or add additional context in comments.

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.