【发布时间】:2011-11-05 20:48:30
【问题描述】:
我的 .js 文件中有一个函数,它调用称为 getStudents 的 web 服务方法:
[WebMethod(Description = "white student list")]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<Job> getStudents(long classId)
{
return (new classManager()).getStudents(classId);
}
方法调用如下:
function loadStudents() {
var parameters = JSON.stringify({ 'classId': 0 });
alert(parameters);
$("#ProcessingDiv").show('fast', function() {
$.ajax({
type: "POST",
url: "myWebService.asmx/getStudents",
data: parameters,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
$("#ProcessingDiv").hide();
var students= response.d;
alert('yes');
},
error: function(request, status, error) {
alert(request.responseText);
}
,
failure: function(msg) {
alert('somethin went wrong' + msg);
}
});
});
}
$(document).ready(function() {
loadStudents();
});
当我调试时,服务 web 方法已成功执行,但我没有收到警报('yes'),也没有 msg 错误。
怎么了?
【问题讨论】:
-
您查看了响应内容吗? (使用 Firebug 或 Chrome 开发工具)
-
使用 firebug,您可以查看您的 ajax 请求,并在响应时得到 - 也许根本没有响应?正如我所假设的,您应该从
Job类中获取带有道具的 JS 对象列表 - 但我没有进入 asp.net,所以我不知道序列化是如何在那里工作的。 -
您的代码看起来正确。黑暗中的一枪是确保您的 Web 服务类(不是方法,而是包含类)具有
[ScriptService]属性。 -
谢谢你们,firebug 解决了我的 pb。它是对象属性中的一个 pb