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.
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;
}
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.