我的项目中有一个Web服务,并使用jquery从另一个页面调用它。 但是没有从Web服务获取数据,我已经在Web服务中使用断点检查它没有被jquery调用命中。我的代码是。
网络服务
namespace DOC_HTML
{
/// <summary>
/// Summary description for JsonService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[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 JsonService : System.Web.Services.WebService
{
[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
public static string DocTypeList()
{
MastersService MS = new MastersService();
DataSet ds = new DataSet();
ds.ReadXml(new XmlNodeReader(MS.DocumentTypeList()));
List<dropDown> listDocType = new List<dropDown>();
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
dropDown objst = new dropDown();
objst.Id = Convert.ToString(ds.Tables[0].Rows[i]["docCode"]);
objst.Name = Convert.ToString(ds.Tables[0].Rows[i]["docName"]);
listDocType.Insert(i, objst);
}
}
JavaScriptSerializer jscript = new JavaScriptSerializer();
return jscript.Serialize(listDocType);
}
}
public class dropDown
{
public string Id { get; set; }
public string Name { get; set; }
}
}
网页代码..
<script type="text/javascript">
$(document).ready(function () {
loadData();
});
function loadData() {
$.ajax({
type: "POST",
url: "../JsonService.asmx/DocTypeList",
data: {},
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
var jsdata = JSON.parse(data.d);
$.each(jsdata, function (key, value) {
$('#<%= ddlDocType.ClientID %>').append($("<option></option>").val(value.Id).html(value.Name));
});
},
error: function (e) {
alert(JSON.stringify(e));
}
});
}
</script>