C# $.ajax未调用WebService
我是jqueryajax新手。我想调用web服务,但不工作。 这是我的jquery代码C# $.ajax未调用WebService,c#,web-services,jquery,C#,Web Services,Jquery,我是jqueryajax新手。我想调用web服务,但不工作。 这是我的jquery代码 $(document).ready(function () { $('#TxBx_BasicSalary').focusout(function () { var EmployeeId = $('#Hid_EmpID').val(); $.ajax({ type: "POS
$(document).ready(function () {
$('#TxBx_BasicSalary').focusout(function () {
var EmployeeId = $('#Hid_EmpID').val();
$.ajax({
type: "POST",
cache: false,
contentType: "application/json; charset=utf-8",
url: '/WebService/IncDedWebService.asmx/GetInceDed',
data: JSON.stringify({ id: EmployeeId }),
dataType: 'json',
success: function (data) {
alert("")
},
error: function () { alert("error"); }
});
});
这是WebService方法
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string GetInceDed(int id)
{
ClsSalary salary = new ClsSalary();
//var abc salary=.GetIncDedByEmpId(id);
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(salary.GetIncDedByEmpId(id));
return json;
}
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string GetInceDed(int id)
{
ClsSalary salary = new ClsSalary();
var abc salary=.GetIncDedByEmpId(id);
string json = "{\"IncDeb\":[\"EmpId\":\"" + abc.EmpId +"\"]"; //And you have to keep going with the other members
return json;
}
这在我调用时不起作用。它执行错误部分。
请帮助我。我做错了什么。尝试以下更改:
$(document).ready(function () {
$('#TxBx_BasicSalary').focusout(function () {
var EmployeeId = $('#Hid_EmpID').val();
$.ajax({
type: "POST",
cache: false,
contentType: "application/json; charset=utf-8",
url: '/WebService/IncDedWebService.asmx/GetInceDed',
data: '{ "id": "' + EmployeeId + '" }', //THIS LINE
dataType: 'json',
success: function (data) {
var emp = $.toJson(data.d); //THIS
alert(emp.IncDeb.EmpID); //AND THIS
},
error: function () { alert("error"); }
});
});
这是WebService方法
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string GetInceDed(int id)
{
ClsSalary salary = new ClsSalary();
//var abc salary=.GetIncDedByEmpId(id);
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(salary.GetIncDedByEmpId(id));
return json;
}
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string GetInceDed(int id)
{
ClsSalary salary = new ClsSalary();
var abc salary=.GetIncDedByEmpId(id);
string json = "{\"IncDeb\":[\"EmpId\":\"" + abc.EmpId +"\"]"; //And you have to keep going with the other members
return json;
}
您尚未发布确切的错误消息,但有几件事需要查找:
$.ajax
调用中指定了POST
,而ScriptMethod
具有UseHttpGet=true
。我假设POST
[System.Web.Script.Services.ScriptService]
才能从ajax调用(根据asmx
代码模板添加的注释)[WebService(Namespace = "http://YourNameSpaceGoesHere/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 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 IncDedWebService : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public string GetInceDed(int id)
{
ClsSalary salary = new ClsSalary();
//var abc salary=.GetIncDedByEmpId(id);
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(new ClsSalary
{
Amount = 1234,
Id = 123,
Name = "Basic Salary"
});
return json;
}
}
public class ClsSalary
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Amount { get; set; }
}
返回的json为:
{"d":"{\"Id\":123,\"Name\":\"Basic Salary\",\"Amount\":1234}"}
手动组装JSON字符串通常不是一个好主意。序列化类和方法的存在是有原因的,并且在一般情况下工作得很好。是的,我知道,但使用.Net只能以这种方式工作,我几周前也遇到了同样的问题,我尝试了在internet上找到的所有方法,但都没有成功。您是否使用浏览器中的开发工具(在IE中按F12,Chrome或Firefox和FireBug)查看请求和返回的内容?您是否查看了错误详细信息?加载资源失败:服务器响应状态为500(内部服务器错误)。我在web浏览器中遇到此错误。这通常是因为代码中引发了异常。尝试直接浏览web服务URL,查看是否从中获得异常跟踪消息。另外,尝试调试web服务,并在
getinceed
方法的开始处放置断点。当我在方法的开始处超过断点时,web服务方法不会被断点击中。