[英]Unable to save and get value from web service session ASMX asp.net on AJAX Call
[英]How to use jQuery to call asp.net asmx web service with parameters to get response
我编写了一个简单的ASMX Web服务,其中提供了一种从数据库中获取一些价值的方法。 我的服务代码是:
[WebMethod]
public string GetProductStock(string productId, string TerminalId, string CCPId)
{
ProductsDomain objProduct = new ProductsDomain();
objProduct.Product_Id = Convert.ToInt32(productId);
objProduct.TerminalId = Convert.ToInt32(TerminalId);
objProduct.CCPId = Convert.ToInt32(CCPId);
DataTable dt = objProduct.GetProductDetail();
if (dt.Rows.Count > 0)
{
return dt.Rows[0]["CurrentQty"].ToString();
}
else
{
return "0";
}
}
这是我的jQuery函数:
function checkStock() {
var txt_PId = $("#content_body_content_lPId").text();
var txt_TerminalId = $("#content_body_content_lTerminalId").text();
var txt_CCPId = $("#content_body_content_lCCPId").text();
var msg = "{" + String.format("'productId':'{0}', 'TerminalId':'{1}','CCPId':'{2}'", txt_PId, txt_TerminalId, txt_CCPId) + "}"
// alert(msg);
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "CService.asmx/GetProductStock",
data: msg,
dataType: "json",
success: function (Result) {
alert();
Result = Result.d;
// data = Result
//alert(data)
},
error: function (Result) {
debugger;
alert("Error: " + Result.error.toString());
return false;
}
});
return false;
}
此jquery方法返回错误,并且当我调试错误时,错误状态代码为500。错误消息:
错误:函数(){if(h){var d = h.length;!function f(b){n.each(b,function(b,c){var d = n.type(c);“ function “=== d a.unique && k.has(C)|| h.push(c)中:C && c.length &&”!?字符串“== d && F(C)})}(参数),b E = h.length :c &&(g = d,j(c))}返回此}
有人可以建议我解决这个问题吗?
必须在继承System.Web.Services.WebService
的class
上启用[System.Web.Script.Services.ScriptService]
,以通过ajax
访问其方法
网络服务
[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 WebServiceFile : System.Web.Services.WebService
{
[WebMethod]
//[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public string GetProductStock(string productId, string TerminalId, string CCPId)
{
ProductsDomain objProduct = new ProductsDomain();
objProduct.Product_Id = Convert.ToInt32(productId);
objProduct.TerminalId = Convert.ToInt32(TerminalId);
objProduct.CCPId = Convert.ToInt32(CCPId);
DataTable dt = objProduct.GetProductDetail();
if (dt.Rows.Count > 0)
{
return dt.Rows[0]["CurrentQty"].ToString();
}
else
{
return "0";
}
}
public class ProductsDomain
{
public int Product_Id { get; set; }
public int TerminalId { get; set; }
public int CCPId { get; set; }
public DataTable GetProductDetail()
{
DataTable dt = new DataTable();
dt.Columns.Add("CurrentQty");
DataRow dr = dt.NewRow();
dr["CurrentQty"] = "Smith";
dt.Rows.Add(dr);
DataRow dr1 = dt.NewRow();
dr1["CurrentQty"] = "John";
dt.Rows.Add(dr1);
return dt;
}
}
}
jQuery的
function checkStock() {
var txt_PId = 1;
var txt_TerminalId = 2;
var txt_CCPId = 3;
var msg = '{productId:"' + txt_PId + '",TerminalId:"' + txt_TerminalId + '",CCPId:"' + txt_CCPId + '"}';
debugger;
$.ajax({
type: "POST",
cache: false,
contentType: "application/json; charset=utf-8",
url: "/WebServiceFile.asmx/GetProductStock",
data: msg,
dataType: "json",
success: function (Result) {
alert();
Result = Result.d;
// data = Result
alert(Result)
},
error: function (Result) {
debugger;
alert("Error: " + Result.error.toString());
return false;
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.