我有一个jQuery ajax调用,如下所示。我按照Better JSON data structure
中的建议进行格式化但是我得到Internal Server Error
。我认为这是因为作为参数传递给服务器方法的JSON数据不合适。知道我们如何解决这个问题吗?
jQuery Ajax
$( "#btnReceive" ).click(function()
{
var containerID = $('#txtContainerId').val();
alert(containerID);
alert('New4');
var containerScanParameter = {};
containerScanParameter.ContainerID = "A";
containerScanParameter.AdvShipNotice = "B";
containerScanParameter.LocationID = "B";
containerScanParameter.UserID = "B";
containerScanParameter.PlantCD = "B";
$.ajax({
type: "POST",
url: "rcvScanTXAdd.aspx/GetResult",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: containerScanParameter,
success: displayResultForLog,
error: errorFunction
});
});
服务器方法
<WebMethod()> _
Public Shared Function GetResult(ByVal containerScanParameter As ContainerScanParameter) As ReceiveScanMessage
Dim receiveScanMessage As ReceiveScanMessage
receiveScanMessage = New ReceiveScanMessage()
Return receiveScanMessage
End Function
参数
Public Class ContainerScanParameter
Private _ContainerID As String
Property ContainerID() As String
Get
Return _ContainerID
End Get
Set(ByVal Value As String)
_ContainerID = Value
End Set
End Property
Private _AdvShipNotice As String
Property AdvShipNotice() As String
Get
Return _AdvShipNotice
End Get
Set(ByVal Value As String)
_AdvShipNotice = Value
End Set
End Property
Private _LocationID As String
Property LocationID() As String
Get
Return _LocationID
End Get
Set(ByVal Value As String)
_LocationID = Value
End Set
End Property
Private _UserID As String
Property UserID() As String
Get
Return _UserID
End Get
Set(ByVal Value As String)
_UserID = Value
End Set
End Property
Private _PlantCD As String
Property PlantCD() As String
Get
Return _PlantCD
End Get
Set(ByVal Value As String)
_PlantCD = Value
End Set
End Property
End Class
请求标题
更新
使用以下方法解决
推荐json2.js
<script type="text/javascript" src="../Javascript/json2.js"></script>
使用Stringify,如下所示
data: JSON.stringify({ containerScanParameter: containerScanParameter }),
CODE
$( "#btnReceive" ).click(function()
{
var containerID = $('#txtContainerId').val();
alert(containerID);
var containerScanParameter = {};
containerScanParameter.ContainerID = "A";
containerScanParameter.AdvShipNotice = "B";
containerScanParameter.LocationID = "B";
containerScanParameter.UserID = "B";
containerScanParameter.PlantCD = "B";
$.ajax({
type: "POST",
url: "rcvScanTXAdd.aspx/GetResult",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify({ containerScanParameter: containerScanParameter }),
success: displayResultForLog,
error: errorFunction
});
});