我已经验证了来自C#Webmethod的JSON响应,所以我不认为这是问题所在.
我试图使用简单的jQuery $.ajax解析结果,但是由于某种原因,我无法获得正确触发并解析结果的方法,而且似乎也无法获取触发结果的函数.它们对可以返回的JSON对象大小的任何限制.
我还从“ Site.Master”页面中删除了此代码,因为当我单击简单按钮时,它将始终刷新.标签是否可以与jQuery元素(例如我从DOM抓取的按钮输入)一起正常使用?
function ajax() {
//var myData = { qtype: "ProductName",query: "xBox" };
var myData = { "request": { qtype: "ProductName",query: "xBox"} };
$.ajax({
type: "POST",url: "/webservice/WebService.asmx/updateProductsList",data: {InputData:$.toJSON(myData)},contentType: "application/json; charset=utf-8",dataType: "json",success: function (msg) {
// var msg = {__type: "Testportal.outputData",id: "li1234",message: "it's work!",myInt:101}
alert("message=" + msg.d.ProductName + ",id=" + msg.d.Brand);
},error: function (res,status) {
if (status === "error") {
// errorMessage can be an object with 3 string properties: ExceptionType,Message and StackTrace
var errorMessage = $.parseJSON(res.responseText);
alert(errorMessage.Message);
}
}
});
}
和页面:
和服务器端Web方法:
public class WebService : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(UseHttpGet = true,ResponseFormat = ResponseFormat.Json)]
public OutputData updateProductsList(InputData request)
{
OutputData result = new OutputData();
var db = new App_Data.eCostDataContext();
var q = from c in db.eCosts
select c;
if (!string.IsNullOrEmpty(request.qtype) && !string.IsNullOrEmpty(request.query))
{
q = q.Like(request.qtype,request.query);
}
//q = q.Skip((page - 1) * rp).Take(rp);
result.products = q.ToList();
searchObject search = new searchObject();
foreach (App_Data.eCost product in result.products)
{
/* create new item list */
searchResult elements = new searchResult()
{
id = product.ProductID,elements = GetPropertyList(product)
};
search.items.Add(elements);
}
return result;
}
和辅助类:
public class OutputData
{
public string id { get; set; }
public List products { get; set; }
}
public class InputData
{
public string qtype { get; set; }
public string query { get; set; }
}