2011-02-07 31 views
3

我在VS2010

[ServiceContract] 
public interface IService1 
{ 

    [OperationContract] 
    string GetData(int value); 



    // TODO: Add your service operations here 
} 



public class Service1 : IService1 
{ 
    public string GetData(int value) 
    { 
     return string.Format("You entered: {0}", value); 
    } 


} 

以下調用開發了一個簡單的WCF服務工作

protected void Button1_Click(object sender, EventArgs e) 
    { 
     ServiceReference1.Service1Client p = new ServiceReference1.Service1Client(); 
     Label1.Text= p.GetData(5); 
    } 

,但是當我試圖打電話它從jquery它不能正常工作

$(".test").live("click", function() { 
    $.ajax({ 
       type: "post", 
       url: "http://localhost/Service1.svc/GetData", 
       data: {value:'1'}, 
       contentType: "application/json; charset=utf-8", 
       timeout: 10000, 
       processData: true, 
       dataType: "json",  
       success: function(d) { 
       alert(d);            
        }, 
        error:function (xhr, ajaxOptions, thrownError){ 
         alert(xhr.status); 
         alert(thrownError.toString()); 
        } 
     }); 

有人可以幫助我因爲它給了我不眠之夜。 在此先感謝。

回答

0

檢查通過線路傳遞的JSON的數據類型(使用Fiddler2或您選擇的瀏覽器的開發人員工具)。我懷疑JSON傳遞的是「1」而不是整數。

看來,問題出在你的數據參數:

data: {value:'1'}, 

這不應該有它周圍的單引號。該服務試圖解析爲帶有字符串類型的「值」變量的方法。改爲:

data: {value:1}, 

這應該有助於服務解決到正確的服務方法。

0

先生,試試這個

$(".test").live("click", function() { 
$.ajax({ 
      type: "post", 
      url: "http://localhost/Service1.svc/GetData", 
      data: "{'value':1}", 
      contentType: "application/json; charset=utf-8", 
      timeout: 10000, 
      processData: true, 
      dataType: "json",  
      success: function(d) { 
      alert(d);            
       }, 
       error:function (xhr, ajaxOptions, thrownError){ 
        alert(xhr.status); 
        alert(thrownError.toString()); 
       } 
    });