따라서이 같은 서비스 호출에 하나 개의 매개 변수를 가지고, 클라이언트 측에서 객체 리터럴을 구축하고 서비스에 그 전체 개체를 전달 고려 :
클라이언트 측 :
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; }
}
마지막으로 변경하려면 웹 서비스 코드는 다음과 같이 하나 개의 매개 변수를 적용합니다 : JQuery와 통화에서 전달 된 데이터는 자동으로 긴 이름이 모두에 일치로 ServiceData
클래스에있는 클래스 속성와 일치한다 : 주
[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();
}
}
}
클라이언트 측과 서버 측.
매개 변수를 단일 개체의 속성으로 반환하거나 단일 배열의 별도 값으로 보냅니다. –