2013-11-25 15 views
0

下面是我的代碼在後面的代碼在ASP.NET Web應用程序要執行AJAX調用ASP.NET Web應用程序

public string GetData(string name) 
    { 
     WCF_Web_Service.Service1 client = new WCF_Web_Service.Service1(); 
     string Name=client.GetData(name); 
     return Name; 
    } 

在這裏,我已經消耗我的簡單的WCF服務應分羣輸出你好,

以下是我的jQuery代碼

function asyncServerCall(name) { 
     jQuery.ajax({ 
      url: 'WebForm1.aspx/GetData', 
      type: "POST", 
      data: "{'name':" + name + "}", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function (data) { 
       alert(data.d); 
      } 
     }); 
    } 

我呼籲在按鈕單擊事件這個函數如下

<input type="button" value="click me" onclick="asyncServerCall(1);" />, 

我想使AJAX調用來調用WCF服務,這種方法當我點擊按鈕,我得到以下錯誤

Failed to load resource: the server responded with a status of 500 (Internal Server Error) 

我是新手使用WCF服務,並進行Ajax調用喚起它的方法,任何幫助將不勝感激....

+0

異常出現時後面的代碼功能'GetData'嘗試的調用WCF或呼叫沒有達到後面的代碼?你有沒有在代碼中加入斷點? –

+0

我照耀處斷點和檢查,我可以看到WCF服務調用,我在字符串名稱中獲得價值,但我不能作出這樣的AJAX調用jQuery的... – Reshma

+0

你的意思是什麼反應?當WCF將數據返回給代碼隱藏或當代碼將數據返回給客戶端時發生問題?當代碼隱藏數據返回給客戶端side..it不是將數據返回到客戶方出現 –

回答

1

您的GetData方法需要是一個靜態的網絡方法,所以改變它。

[System.Web.Services.WebMethod] 
public static string GetData(string name) 
{ 
    WCF_Web_Service.Service1 client = new WCF_Web_Service.Service1(); 
    string Name=client.GetData(name); 
    return Name; 
} 

Here's a good article on the why's但本質上這是因爲:

This is exactly why they must be marked as static. They cannot interact with the instance properties and methods of your Page class, because a page method call creates no instance of the Page or any of its controls.

相關問題