2014-02-25 54 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',的伎倆... :)