我有一个看起来像这样的网络方法:
[WebMethod]
public void InsertDrugNameAndColorToDatabase(string drugName,string drugColor)
{
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
using (var con = new SqlConnection(cs))
{
using (var cmd = new SqlCommand("spInsertDrugText", con))
{
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@drugName", drugName);
cmd.Parameters.AddWithValue("@drugColor", drugColor);
cmd.ExecuteNonQuery();
}
}
}
和一点JS:
<script type="text/javascript">
$(document).ready(function () {
$(".drugQuizzes").draggable({ tolerance: "fit" });
$(".drugAnswers").droppable({
drop: function (event, ui) {
var drugName = JSON.stringify({ "drugName": $(ui.draggable).find("span").text() });
var drugColor = JSON.stringify({ "drugColor": $(ui.draggable).css("background-color") });
console.log(drugColor);
console.log(drugName);
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "SendDrugName.asmx/InsertDrugNameToDatabase",
data: drugName,
dataType: "json",
success: function (data) {
//response(data.d);
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(thrownError);
}
});
}
});
});
</script>
<script type="text/javascript">
$(document).ready(function () {
$(".drugQuizzes").draggable({ tolerance: "fit" });
$(".drugAnswers").droppable({
drop: function (event, ui) {
var drugName = JSON.stringify({ "drugName": $(ui.draggable).find("span").text() });
var drugColor = JSON.stringify({ "drugColor": $(ui.draggable).css("background-color") });
console.log(drugColor);
console.log(drugName);
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "SendDrugName.asmx/InsertDrugNameToDatabase",
data: drugName,
dataType: "json",
success: function (data) {
//response(data.d);
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(thrownError);
}
});
}
});
});
</script>
我有一个存储过程和JS的版本,其中只有一个参数被发送到存储过程,这是有效的。 从console.log(drugName)和console.log(drugColor)我得到
{"drugColor":"rgb(255, 69, 0)"}
{"drugName":"ORACEA"}
如何让ajax调用的data
参数一次性获取多个参数?
使用jQuery ajax一次向存储过程发送多个参数需要注意哪些常用技术名称?
答案 0 :(得分:1)
您可以指定多个数据项,例如:
data: 'drugName='+ drugName + '&drugColor=' + drugColor;
答案 1 :(得分:1)
您根本不需要对其进行字符串化,您可以将参数作为对象传递。试试这个:
$(".drugAnswers").droppable({
drop: function (event, ui) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "SendDrugName.asmx/InsertDrugNameToDatabase",
data: {
'drugName': $(ui.draggable).find("span").text(),
'drugColor': $(ui.draggable).css("background-color")
},
dataType: "json",
success: function (data) {
//response(data.d);
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(thrownError);
}
});
}
});
然后ajax()
函数将为您进行字符串化并将其传递给您的C#端点。
答案 2 :(得分:1)
考虑在客户端构建对象文字并将整个对象传递给服务,因此在服务调用中有一个参数,如下所示:
客户端:
var myData = {};
myData.DrugName' = $(ui.draggable).find("span").text();
myData.DrugColor' = $(ui.draggable).css("background-color");
// Create a data transfer object (DTO) with the proper structure, which is what we will pass to the service.
var DTO = { 'theData' : myData };
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "SendDrugName.asmx/InsertDrugNameToDatabase",
data: JSON.stringify(DTO),
dataType: "json",
success: function (data) {
//response(data.d);
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(thrownError);
}
});
现在在服务端,您需要构建一个表示上面创建的对象文字内容的类,如下所示:
public class ServiceData
{
public string DrugName { get; set; }
public string DrugColor { get; set; }
}
最后,更改您的Web服务代码以接受一个参数,如下所示:
[WebMethod]
public void InsertDrugNameAndColorToDatabase(ServiceData theData)
{
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
using (var con = new SqlConnection(cs))
{
using (var cmd = new SqlCommand("spInsertDrugText", con))
{
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@drugName", theData.DrugName);
cmd.Parameters.AddWithValue("@drugColor", theData.DrugColor);
cmd.ExecuteNonQuery();
}
}
}
注意:只要名称在客户端和服务器端都匹配,从jQuery调用传递的数据就会自动与ServiceData
类中的类属性匹配。