Pages

Monday, January 17, 2011

JSON via webservices Using JSONP

//Step1  add ScriptService  for the class & [ScriptMethod(UseHttpGet = true)]
//to the method
/// 
/// Summary description for RatingService_JSONP
/// 
[WebService(Namespace = "http://ws.cli.det.nsw.edu.au/ns/Web2Services/RattingService",
           Description = "Ratting service, this service will be plugable in any application, to support rating with comments, including reports and summaries")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class RatingService_JSONP : RatingService
{
    }
    [WebMethod(Description = @"Sample Please remove it")]
    [ScriptMethod(UseHttpGet = true)]
    public string[] Sample()
    {
        return new string[] { "AAA""BBB""CCC""DDD" };
    }
 
}
 
//Step2 
// use format=json  for json format
// use callback=?    for JSONP
  
 $(document).ready(function () {
            $("#btnlookup").click(function () { lookup(); });
        });
        function lookup() {
            var addressURL = "http://localhost/Web2Services/WebServices/JSON/RatingService_JSONP.asmx/Sample?format=json&callback=?";
            var outputStr = "";
            $("#output").text(outputStr);
            $.getJSON(addressURL, nullfunction (response) {
                $.each(response.d, function (i, val) {
                    outputStr = "
" + val.toString() + "
"
;                                          $(outputStr).appendTo("#output");                 });             });     // getJson

No comments:

Post a Comment