繁体   English   中英

通过Ajax和JSON调用WCF REST服务

[英]calling WCF REST service through Ajax and JSON

我在WCF(demo)中做了一个Rest服务,它给我的输出为: {"GetEmployeeJSONResult":{"Id":101,"Name":"Sumanth","Salary":5000}}

现在,我在asp.net中创建了一个网站,我在其中通过AJAX JSON调用此休息服务...

我的代码如下:

<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>

您可能会看到我已经通过AJAX和getJSON方法访问了服务。

现在,当我在写数据的警示,它让我看到输出不确定..我曾尝试: alert(result.d.length) , alert(result.d.GetEmployeeJSONResult)但始终显示我作为undefined..in两种方法..

我的WCF服务代码如下:

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;


       }


 }
}

请帮我解决这个问题。

预先感谢您。

Krunal

使用WebGet更改WebInvoke并删除Method="GET"

并在AJAX中调用。

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

试试吧。

此功能应该起作用:

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);
            }
        });
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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