前台:ShowDetail("英伦学院派马夹");
function ShowDetail(productId) {
// make an ajax call to the web service to get Car detail
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: "ProductService.asmx/GetProductByIds",
data: "{Productname:" + productId + "}",
dataType: "json",
success: SelectProductByIdSuccess,
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown + ':' + textStatus);
}
});
后台: /// <summary>
/// 根据商品名称查询商品
/// </summary>
/// <param name="id">商品名称</param>
/// <returns>商品</returns>
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
public List<Product> GetProductByIds(string Productname)
{
return dataSource.GetProductByName(Productname);
}
}
public List<Product> GetProductByName(string name)
{
return productList.Where(p => p.ProductName == name).ToList();
}
private List<Product> productList = new List<Product>()
{
new Product(){ProductId=1,ProductName="笔记本", Price=10000M, Stock=10},
new Product(){ProductId=2,ProductName="格子绒长袖衬衫", Price=90M, Stock=20},
new Product(){ProductId=3,ProductName="纯棉长袖T恤", Price=99M, Stock=40},
new Product(){ProductId=4,ProductName="炫彩T恤", Price=67M, Stock=30},
new Product(){ProductId=5,ProductName="直筒裤男牛仔裤", Price=100M, Stock=20},
new Product(){ProductId=6,ProductName="[无印严选]纯色V领长袖T恤", Price=67M, Stock=50},
new Product(){ProductId=7,ProductName="英伦学院派马夹", Price=44M, Stock=40},
new Product(){ProductId=8,ProductName="纯棉连帽宽松卫衣", Price=66M, Stock=30},
new Product(){ProductId=9,ProductName="纯棉多口袋工装裤", Price=80M, Stock=990},
new Product(){ProductId=10,ProductName="假两件长袖T恤", Price=89M, Stock=30},
};
调试错误:{"Message":"Web 服务调用无效,参数“Productname”缺少值。","StackTrace":" 在 System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters)\r\n 在 System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)\r\n 在 System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n 在 System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}
你返回的不是json对象,是list对象,尝试把list对象改为string的json对象看看
function SelectProductByIdSuccess(result) { var product = eval(result["d"][2]); $("table#tblProductDetail").find("tr").remove(); $('table#tblProductDetail > tbody:last').append('<tr><td>商品名称</td><td>' + product.ProductName + '</td></tr>' + '<tr><td>价 格</td><td>' + product.Price + '</td></tr>' + '<tr><td>库存量</td><td>' + product.Stock + '</td></tr>'); $("a#productLink").trigger('click');
} }
@小明同学: 你这是要干啥?
function ShowDetail(productId) { // make an ajax call to the web service to get Car detail // var data = { productId: productId }; var data = { id: productId }; //id与web服务里 GetProductById(object id)形参id一致,否则报错:无参数值。 $.ajax({ type: "GET", contentType: "application/json; charset=utf-8", url: "ProductService.asmx/GetProductById", dataType: "json", data: data, success: SelectProductByIdSuccess, error: function (XMLHttpRequest, textStatus, errorThrown) { alert(errorThrown + ':' + textStatus); } }); function SelectProductByIdSuccess(result) { var product = eval(result["d"]); $("#tblProductDetail").find("tr").remove(); $('#tblProductDetail > tbody:last').append('<tr><td>商品名称</td><td>' + product.ProductName + '</td></tr>' + '<tr><td>价 格</td><td>' + product.Price + '</td></tr>' + '<tr><td>库存量</td><td>' + product.Stock + '</td></tr>'); $("a#productLink").trigger('click'); }
楼主是看了 灵动生活 的博客吧。我也下了他的demo,里面关于有参数的ajax问题主要出在json里的“data”。我看你改过了,你应该是拼接data有问题。你可以按我的方式试试。我试过了是成功的。注意我标红色的地方,也要改过来,不然你的product是undefined。