0

I have a jQuery ajax call as listed below. I am formatting as suggested in Better JSON data structure

However I am getting an Internal Server Error. I think this is because the JSON data passed as argument to the server method is not proper. Any idea how we can fix this?

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
                    });
        });

Server Method

    <WebMethod()> _
    Public Shared Function GetResult(ByVal containerScanParameter As ContainerScanParameter) As ReceiveScanMessage

        Dim receiveScanMessage As ReceiveScanMessage
        receiveScanMessage = New ReceiveScanMessage()

        Return receiveScanMessage

    End Function

Parameter

    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

Request Headers

enter image description here

UPDATE

Resolved using following approach

  1. Referred json2.js

            <script type="text/javascript"  src="../Javascript/json2.js"></script>  
    
  2. Used Stringify as listed below

     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
                    });
        });

Community
  • 1
  • 1
LCJ
  • 22,196
  • 67
  • 260
  • 418
  • I strongly doubt that an Internal Server Error is being triggered by bad data being passed in. If so, the server code is not doing a good job of validating before processing - do you have control over the server code? – random_user_name Jun 25 '14 at 14:00
  • @cale_b I have access to server code - but during debugging it is not reaching server code. I guess this is due to serialization issue while sending request. When I use one string as parameter, it is working fine.. – LCJ Jun 25 '14 at 14:02
  • Are you actually hitting your `GetResult` method on the server-side? Can you set a breakpoint and check if `containerScanParameter` actually contains anything? On the AJAX side, you might try changing the `data` parameter to something like `data: { containerScanParameter : containerScanParameter }`. – Matt Burland Jun 25 '14 at 14:02
  • @MattBurland. Not hitting breakpoint... Even when I updated as per your comment, it is not resolved... Updated the question with the details. – LCJ Jun 25 '14 at 14:08
  • You are correct sir. You are not passing *json*, you are instead passing a *javascript object*. If you want to pass json, you will have to convert your javascript object into json. – Kevin B Jun 25 '14 at 14:10
  • 1
    http://stackoverflow.com/questions/191881/serializing-to-json-in-jquery/912247#912247 – Kevin B Jun 25 '14 at 14:13
  • 1
    @Lijo: JSON.stringify your `data` – Matt Burland Jun 25 '14 at 14:21

0 Answers0