2014-02-25 126 views
0

,我写我的第一个WCF服务WCF服务不返回数据的JSON

这里是服务方法

public Employee GetEmployee() 
    { 
     Employee objEmp = new Employee(); 
     objEmp.EmpName = "Jay"; 
     objEmp.EmpAddress = "Delhi"; 
     return objEmp; 
    } 

这里是合同

[OperationContract] 
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)] 
    Employee GetEmployee(); 

这里是jQuery的AJAX调用

$(function() { 
     $.ajax({ 
      type: 'GET', //GET or POST or PUT or DELETE verb 
      url: 'Service1.svc/GetEmployee', // Location of the service 
      contentType: "application/json; charset=utf-8", // content type sent to server 
      dataType: 'json', //Expected data format from server 
      success: function (msg) {//On Successfull service call 
       alert('success'); 
       console.log(msg); 
      }, 
      error: function (jqXHR, textStatus, errorThrown) { 
       console.log(jqXHR, textStatus, errorThrown); 
      } 
     }); 
    }); 

它工作得很好,不错,但我没有得到数据,JSON,但作为一个对象

+1

你怎么回来的? –

+1

这几天我推荐使用WEB Api来处理这些场景 – TGH

+0

为什么你需要它作为JSON字符串。 JSON对象比字符串更容易解析。 – SajithNair

回答

0

更改数据类型来dataType: 'text',的伎俩... :)