2010-08-11 262 views
0

我剛剛開始使用JQuery庫,所以如果我缺少一些明顯的東西,請耐心等待。我有一對夫婦的測試方法webserivce ...JQuery Ajax 500內部錯誤

[WebService(Namespace = "http://localhost/WebServices")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService] 
public class SystemServices : BaseWebService 
{ 
    [WebMethod(EnableSession = true)] 
    public string GetDate() 
    { 
     return DateTime.Today.ToShortDateString(); 
    } 
    [WebMethod(EnableSession = true)] 
    public string PerformPISearch(string firstName, string lastName) 
    { 
     return firstName + lastName; 
    } 

我可以用$就要求使用不帶參數沒有問題getDate方法,但我得到了500內部服務器錯誤當我嘗試運行PerformPISearch方法(Web服務構造函數永遠不會被擊中)時從jQuery返回...因此,我假設我正在嘗試將參數傳遞給該方法的方式出錯,但我可以' W圖什麼?

 function PerformSearch() { 
    var strFirstName = (txtFirstName.GetValue() == null ? "" : txtFirstName.GetValue()); 
    var strLastName = (txtLastName.GetValue() == null ? "" : txtLastName.GetValue()); 
    var webserviceURL = '<%= WebServiceURL %>' 

    $.ajax({ 
     type: "POST", 
     url: webserviceURL + "SystemServices.asmx/PerformPISearch", //Can change to use GetDate and it works. 
     data: ({firstName: strFirstName, lastName: strLastName}), //thinking the problem is here 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function(msg) { 
      AjaxSucceeded(msg); 
     }, 
     error: AjaxFailed 
    }); 
} 

function AjaxSucceeded(result) { 
    alert(result.d); 
} 
function AjaxFailed(result) { 
    alert(result.status + ' ' + result.statusText); 
} 

回答

3

您是否嘗試過刪除 「()」:

data: {firstName: strFirstName, lastName: strLastName} 

或者把一切都變成字符串:

data: "{'firstName': '" +strFirstName + "', 'lastName': '" +strLastName+ "'}" 
+0

數據: 「{ '的firstName': '」 + strFirstName + 「 ' 'lastName的':'」 + strLastName + 「'}」 我們贏家,謝謝... – AGoodDisplayName 2010-08-11 21:10:04

+0

你有沒有試過$ .post?我用它來與asp.net的MVC,可以減少代碼,如果它的工作原理:) $。員額(webserviceURL + 「SystemServices.asmx/PerformPISearch」, { 姓:strFirstName, 名字:strLastName }, 功能(結果){ \t //做某事 }) – mathieu 2010-08-11 21:15:26

+0

不,我沒有,但我會看看它。再次感謝。 – AGoodDisplayName 2010-08-12 16:17:45