我刚刚开始使用JQuery库,所以如果我遗漏了一些明显的东西,请耐心等待。我有一个带有几种测试方法的webserivce ......
[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;
}
我可以使用$ .ajax请求来使用没有参数但没有问题的GetDate方法,但是当我尝试运行PerformPISearch方法时,我从jQuery返回500内部服务器错误(Web服务构造函数从不受到打击)...所以我假设我在尝试将参数传递给方法的方式有问题,但我无法弄清楚是什么......
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);
}
答案 0 :(得分:3)
您是否尝试删除“()”:
data: {firstName: strFirstName, lastName: strLastName}
或将所有内容放入字符串中:
data: "{'firstName': '" +strFirstName + "', 'lastName': '" +strLastName+ "'}"