简体   繁体   中英

calling WCF REST service through Ajax and JSON

I have made one Rest service in WCF (demo) , which gives me output as : {"GetEmployeeJSONResult":{"Id":101,"Name":"Sumanth","Salary":5000}}

Now i have created one website in asp.net in which i am calling this rest service through AJAX JSON...

my code is as below:

<script type="text/javascript">
    $(document).ready(function () {
        //            $.getJSON("http://localhost/SampleService/Service1.svc/getJson?callback=?", null, function (data) {
        //                alert(data.Name);
        //            });

        var endpointAddress = "http://localhost/SampleService/Service1.svc";
        var url = endpointAddress + "/GetJson";
        $.ajax({
            type: 'GET',
            url: url,
            dataType: 'jsonp',
            contentType: 'application/json',
            data: "{}",
            success: function (result) {
                //alert(JSON.stringify(result));
                alert(result.length);
            },
            error:function(jqXHR)
            {
                alert(jqXHR.status);
            }
        });

    });
</script>

You could see that i have accessed service through both AJAX and getJSON methods..

now when i am making a alert of data , it shows me output as undefined.. i have tried : alert(result.d.length) , alert(result.d.GetEmployeeJSONResult) but always shows me as undefined..in both methods..

My WCF service code is as below:

namespace WcfServiceXmlAndJsonDemo
{

[ServiceContract]
public interface IService1
{
    #region OperationContracts

    [OperationContract]
    [WebInvoke(Method="GET",UriTemplate="GetXml",ResponseFormat=WebMessageFormat.Xml,RequestFormat=WebMessageFormat.Xml,BodyStyle=WebMessageBodyStyle.Bare)]
    EmployeeXML GetEmployeeXML();

    [OperationContract]
    [WebInvoke(Method = "GET", UriTemplate = "GetJson", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
    List<EmployeeJSON> GetEmployeeJSON();

    #endregion
}
}

DataContract EmployeeJSON :

namespace WcfServiceXmlAndJsonDemo
{

[DataContract]
public class EmployeeJSON
{
    #region Properties

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public int Id { get; set; }

    [DataMember]
    public double Salary { get; set; }

    #endregion
}
}

Service1.svc.cs :

 namespace WcfServiceXmlAndJsonDemo
 {   


  public class Service1 : IService1
  {    

      public List<EmployeeJSON> GetEmployeeJSON()
       {
           EmployeeJSON json = new EmployeeJSON()    
           {Name="Sumanth",Id=101,Salary=5000.00 };
          return json;


       }


 }
}

Please help me out how to handle this..

Thanking you in advance.

Krunal

Change WebInvoke with WebGet and remove Method="GET" .

And in AJAX call.

type: 'GET',
url: url,
dataType: 'jsonp',
//contentType: 'application/json', No need to mention Content-Type.

Try it.

This function should work:

function () {
        $.ajax({
            type: "GET",
            async: false,
            contentType: "application/json; charset=utf-8",
            url: 'http:www.svc/Service1.svc/GetJson',
            data: "{ }",
            processData: false,
            dataType: "json",               
            success: function (data) {                    
                var result = data.GetEmployeeJSONResult;
                var id = result.Id;
                var name = result.Name;
                var salary = result.Salary;
                $('#jsonData').html('');
                $('#jsonData').append('<table border="1"><tr><th>Employee Id</th><th>Name</th><th>Salary</th></tr><tr><td>' + id + '</td><td>' + name + '</td><td>' + salary + '</td></tr></table>');

            },
            error: function (xhr) {
                alert(xhr.responseText);
            }
        });
    }

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM