gpt4 book ai didi

javascript - 如何使用 javascript 使用来自 asp.net webservice 的 Json 数据

转载 作者:行者123 更新时间:2023-11-28 05:20:56 25 4
gpt4 key购买 nike

从前,有一个人正在从事一个项目。该项目有一个母版页和一个内容页。内容页面有一个下拉列表和两个文本框。然而,当该人从下拉列表中选择任何产品名称时,多么奇怪,它的 totalQuantitypricePerItem 值并没有出现在文本框中!他试图为这个项目编写 Web 服务代码和 javascript 代码,但不幸的是,它没有实现他所设想的应该做的和将会做的。因此,他请求你的帮助。

 public class QuantityAndPrice
{
public string totalQuantity { get; set; }
public string pricePerItem { get; set; }
}

webservice code

  public class QuantityAndPriceService : System.Web.Services.WebService
{

[WebMethod]
public void GetQuantityAndPrice(string productName)
{
QuantityAndPrice quantityAndpriceObject = new QuantityAndPrice();

string connect_string = "datasource=localhost;port=3306;username=root;password=root;";
MySqlConnection connection = new MySqlConnection(connect_string);
string query = "select totalQuantity,pricePerItem from smart_shop.inventory where name='" + productName + "';";
MySqlCommand cmd = new MySqlCommand(query, connection);
connection.Open();
MySqlDataReader reader = cmd.ExecuteReader();



while (reader.Read())
{
quantityAndpriceObject.totalQuantity = reader.GetString("totalQuantity");
quantityAndpriceObject.pricePerItem = reader.GetString("pricePerItem");
}

JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Write(js.Serialize(quantityAndpriceObject));


}
}

javascript

 <script type="text/javascript">

$(document).ready(function () {
$('#productNameDDL').change(function () {

var pName = $('#productNameDDL').val();

$.ajax({

url: 'QuantityAndPriceService.asmx/GetQuantityAndPrice',
data: { productName: pName },
method: 'post',
dataType: 'json',
success: function (data) {

$('#tbxAvailableQuantity').val(data.totalQuantity);
$('#tbxPricePerItem').val(data.pricePerItem);
},
error: function (err) {
alert(err);
}
});
});
});


</script>

and here aspx code

<div class="panel-body">
<div class="row">
<div class="col-md-6">
<h6>&nbsp;&nbsp;Available Qty</h6>
<asp:TextBox ID="tbxAvailableQuantity" CssClass="form-control" ReadOnly="true" runat="server"></asp:TextBox>
</div>

<div class="col-md-6">
<h6>&nbsp;&nbsp;Price/Item</h6>
<asp:TextBox ID="tbxPricePerItem" CssClass="form-control" ReadOnly="true" runat="server"></asp:TextBox>
</div>

</div>
<div class="row">
<div class="col-lg-6">
<h6>&nbsp;&nbsp;Sales Qty</h6>
<asp:TextBox ID="tbxSalesQtuantity" CssClass="form-control" runat="server"></asp:TextBox>
</div>
<div class="col-lg-6">
<h6>&nbsp;&nbsp;Total Price</h6>
<asp:TextBox ID="tbxTotalPrice" CssClass="form-control" runat="server"></asp:TextBox>
</div>
</div>

</div>




<asp:DropDownList ID="productNameDDL" CssClass="form-control" runat="server"></asp:DropDownList>

最佳答案

  1. Web服务类应该有 ScriptService属性。
  2. Web 服务方法应具有 ScriptMethod属性来指定 ResponseFormat = ResponseFormat.Json
  3. 在 java 脚本中,应指定 contentType: "application/json; charset=utf-8"dataType: 'json'
  4. ajax 调用成功部分的结果包含在 d 中,例如就像这样data.d.totalQuantity
  5. ajax 调用中的数据应进行字符串化,例如像这样 data:JSON.stringify({ ProductName: pName })
<小时/>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class QuantityAndPriceService : WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public QuantityAndPrice GetQuantityAndPrice(string productName)
{
QuantityAndPrice quantityAndpriceObject = new QuantityAndPrice
{
totalQuantity = "totalQuantityValue",
pricePerItem = "pricePerItemValue"
};
return quantityAndpriceObject;
}
}

$.ajax({
url: 'QuantityAndPriceService.asmx/GetQuantityAndPrice',
data: JSON.stringify({ productName: pName }),
method: 'post',
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (data) {
$('#tbxAvailableQuantity').val(data.d.totalQuantity);
$('#tbxPricePerItem').val(data.d.pricePerItem);
},
error: function (err) {
alert(err.responseText);
}
});

关于javascript - 如何使用 javascript 使用来自 asp.net webservice 的 Json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40562985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com