日期:2014-05-18  浏览次数:20668 次

asp.net ajax WebService.asmx
js代码不能将数据发送到WebService1.asmx中对应的方法,只能进入insertCallbackSelsectSma提示错误的函数,以下是代码
js代码:
 function GetClassByItemIdcy(id) {
  var data = "{id:" + parseInt(id) + "}";
  //alert(data);
  $.ajax({
  type: "POST",
  contentType: "application/json; charset=utf-8",
  url: "WebService1.asmx/SelectAllClassByItemId",
  data: data,
  dataType: "json",
  success: insertCallbackSelsectSma,
  error: errorCallbackSelsectSma
  });
  }
  function insertCallbackSelsectSma(result) {
  $("#DropDownList1").find("option").remove();
  var i = result.d;
  //alert(i);
  if (result["d"].length > 0) {
  for (var i = 0; i < result["d"].length; i++) {
  var classsma = eval(result["d"][i]);
  alert(classsma.Class_Id);
  $("#DropDownList1").append('<option value="' + classsma.ClassId + '">' + classsma.ClassName + '</option>');
  }
  }
  }
  function errorCallbackSelsectSma(XMLHttpRequest, textStatus, errorThrown) {
  alert("错误1");
  }

页面中的dropdownlist

  &nbsp;&nbsp;<asp:DropDownList ID="DropDownList2" runat="server" onchange="GetClassByItemIdcy(this.value)">

WebService1.asmx 中的代码
[WebMethod]
  [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  public List<ClassEntity> SelectAllClassByItemId(int id)
  {
  classbllall = new ClassBll();
  return classbllall.ClassSelectIdById(id);
  }


------解决方案--------------------
楼主我建议你断点调试一下。
------解决方案--------------------
不要使用不靠谱的jQuery调用,asp.net ajax本来就支持调用asmx。

http://msdn.microsoft.com/zh-cn/library/bb398998(v=vs.90).aspx
http://msdn.microsoft.com/zh-cn/magazine/cc163499.aspx


------解决方案--------------------
当跨域访问时,你这里可以使用自己站点做个“中转”,使用自己的asmx来访问跨域的asmx,然后自己的网页访问自己网站的asmx。