정의 된 페이지 메소드에 객체를 전달하려고합니다. 세 개의 텍스트 상자에서 수집 한 데이터를 전달하려고합니다.jquery ajax를 통해 WebMethod에 객체 전달
페이지 방법
[System.Web.Services.WebMethod]
public static string saveDataToServer(object csObj)
{
// Function Body.
}
자바/jQuery를
$("#osmSendMsg").click(function() {
debugger;
var cntDetails = {
cntName : $("#osmContactName").val(),
cntEmail : $("#osmContactEmail").val(),
cntMsg : $("#osmContactMessage").val()
}
PostDataToServer(cntDetails,"Please wait...","/ContactUs.aspx/saveDataToServer","csObj");
});
PostDataToServer
// Post data to server (ajax call).
function PostDataToServer(dataToSend, strMessagetoShow, strMethodToCall, jsonObjectName) {
debugger;
/*
dataToSend Contains the JSON Object.
submitType 1 == Normal Submit; 2 == Submit and Print.
strMessagetoShow Text that is displayed in the Please Wait Window.
*/
var tempurl = strMethodToCall;
var tempdata;
$.ajax({
url: tempurl,
type: "POST",
async: false,
dataType: "json",
data: "{" + jsonObjectName + ":" + JSON.stringify(dataToSend) + "}",
//timeout: 30000,
contentType: "application/json; charset=utf-8",
success: function (data) {
tempdata = data;
},
error: function (result) {
tempdata = null;
}
}); //end of the ajax call
return tempdata;
} //End of the post Data
N 호출이 웹 메소드에 도달했습니다. 문제 없어. 나는 물건도 가지고있다. 그러나 물건을 어떻게 처리해야합니까?
당신이 볼 수 있듯이, 그게 내가 갖는거야. 또한 클래스를 선언하고 매개 변수로 전달하려고 시도했지만 모든 속성이 비어 있습니다. 데이터가 키로 나타나면 값 쌍을 표시합니다. 사전으로 변환 할 수는 있지만 복잡한 해결책이라고 생각합니다.
간단한 해결책
환영 할 것입니다!
은 참조 http://stackoverflow.com/questions/13175652/json- 포스트 백 - C - 샤프 - 웹 메쏘 드 - 리터럴 - 컨트롤 추가 – Alex
좋습니다. 문자열로 보내고 서버 측에서 분할합니다. –