2012-06-30 30 views
0

嗨我試圖訪問一個WCF服務,該服務使用JQuery返回一個JSON數組,但它不工作。但是,當我使用一個在互聯網上的PHP服務,它的工作原理。 請告訴我我哪裏錯了?從Jquery使用JSON訪問WCF

我的C#類

[ServiceContract] 
public interface IService1 
{ 
    [OperationContract] 
    [WebInvoke(Method = "GET",RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)] 
    Employee[] getData(); 
} 


[DataContract] 
public class Employee 
{ 
    [DataMember] 
    public string id { get; set; } 

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

當我在瀏覽器中加載它,我得到的迴應

[{"id":"1","name":"test"},{"id":"2","name":"test"}] 

的URL,PHP Web服務 http://shell.loopj.com/tokeninput/tvshows.php

我的HTML代碼

<script type="text/javascript"> 
     $(document).ready(function(){ 

    $.ajax({ 
     url: "http://localhost:51220/Service1.svc/getdata", 
     success: function(result){ 
     alert(result); 
     }, 
     dataType: "jsonp" 
    }); 
    }); 
</script> 

當我使用這個我得到的錯誤0,但是當我使用PHP服務,我得到的數組。

+0

爲什麼jsonp在本地主機?你也可以在你的配置文件中顯示你的端點嗎? –

回答

1

請嘗試下面的代碼。

$.ajax({ 
       type: "GET", 
       url: "http://localhost:51220/Service1.svc/getdata", 
       processData: false, 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (result) { 
        alert(result); 
       }, 
       error: function (jqXHR, textStatus, errorThrown) { 
        alert(textStatus + '/' + errorThrown); 
       } 
      })