asp.net jquery datatables.net webmethod
-
20-09-2019 - |
문제
ASP.NET 3.5를 사용하고 있습니다. jQuery를 사용하여 웹 메드를 호출하면 유효한 JSON 데이터를 반환합니다. 그러나 Datatables.net JQuery 플러그인을 사용하여 HTML 테이블을 채우기 위해 동일한 웹 메드를 호출 할 때 페이지의 전체 HTML을 다시 얻습니다.
**WebMethod:**
<WebMethod()> _
Public Shared Function GetData() As String
Dim a As String = "{""aaData"": [['Trident','Internet Explorer 4.0']]}"
Return a
End Function
**Successful JQuery call:**
$("#Result").click(function() {
$.ajax({
type: "POST",
url: "Default2.aspx/GetData",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Replace the div's content with the page method's return.
$("#Result").text(msg.d);
}
});
});
});
실패한 jQuery 호출 :
$(document).ready(function() {
$('#example').dataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "Default2.aspx/GetDate",
"fnServerData": function(sSource, aoData, fnCallback) {
$.ajax({
"dataType": 'json',
"url": sSource,
"data": aoData,
"success": fnCallback
});
}
});
});
두 번째 통화가 HTML을 반환하는 이유에 대한 생각이 있습니까? 두 번째 Ajax 호출에 "Application/JSON; charset = utf-8"을 추가하려고 시도했습니다. 오류가 발생합니다.
해결책
ContentType : Application/JSON이 필요합니다. 공급할 때 얻는 오류는 무엇입니까?
아마도 인코딩 오류가있을 수 있습니다. 보다 .getjson jQuery에서 인코딩을 설정하는 방법
다른 팁
존재하지 않는 메소드를 호출 할 수 있으므로 오류 페이지가 될 수 있습니다. 응답에 무슨 일이 일어나고 있는지 확인하십시오.
"sAjaxSource": "Default2.aspx/GetDate",
성공적인 전화에서는 getData 메소드를 사용하고 있습니다
url: "Default2.aspx/GetData",
실패한 전화로 GetDate Method를 호출합니다.
제휴하지 않습니다 StackOverflow