Not able to pass correct values to REST from check

2019-09-15 14:15发布

问题:

I am trying to learn web development and trying to do some basic checkbox tutorial but I am stuck at sending values to Rest Service.

My intention is if for example two of the checkboxes are selected I should be able to capture that at my Rest Service and perform certain operation.

Below is the HTML FILE

<!DOCTYPE html>
<html>
<body>
<form>
<input type="checkbox" id = "1" name="graphId" value="1">Graph1<br> 
<input type="checkbox" id = "2" name="graphId" value="2">Graph2<br> 
<input type="checkbox" id = "3" name="graphId" value="3">Graph3<br> 
<input type="checkbox" id = "4" name="graphId" value="4">Graph4<br> 
<input id="btnGetResponse" type="button" value="ClickMe!"/> 
</form>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$("#btnGetResponse").click(function()
{
  var ids = $('[name="graphId"]:checked').map(function() {
    return this.id;
  }).get();

  console.log(ids);
  $.ajax({
    type: "GET",
    url: "http://localhost:51349/SMS_Rest.svc/v1/CheckBox",
    data: {
      graphId: ids
    },
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(response)
    {
      if (response == true)
      {
        alert('hello');
      }
    },
    failure: function(response) 
    {
        alert('fail');
    }
  });
});
</script>
</body>
</head>

The URL that gets generated if I select ONE checkbox is :

http://localhost:51349/SMS_Rest.svc/v1/CheckBox?graphId%5B%5D=1

The URL that gets generated if I select TWO checkbox is :

 http://localhost:51349/SMS_Rest.svc/v1/CheckBox?graphId%5B%5D=1&graphId%5B%5D=2 

What changes do I need to make in REST to capture all selected checkboxes. How should I capture this in my REST Service?

Rest Interface:

<ServiceContract()>
Public Interface iSMS_Rest
    <OperationContract(Name:="CheckBox")>
    <WebInvoke(Method:="GET", BodyStyle:=WebMessageBodyStyle.Bare, ResponseFormat:=WebMessageFormat.Json, UriTemplate:="CheckBox?graphId%5B%5D={c_Id}")>

    Function CheckBox(ByVal c_Id As Integer) As Boolean

End Interface

Rest Implementation:

 Public Function CheckBox(ByVal c_Id As Integer) As Boolean Implements iSMS_Rest.CheckBox
        'Some Logic
        If c_Id = 1 Then
            Return True
        Else
            Return False
        End If
    End Function

QUESTION: Do I need to change the way I am passing the checkbox mapped values from the HTML File. Or I need to change the way I am capturing the Request at UriTemplate. My UriTemplate is: <WebInvoke(Method:="GET", BodyStyle:=WebMessageBodyStyle.Bare, ResponseFormat:=WebMessageFormat.Json, UriTemplate:="CheckBox?graphId%5B%5D={c_Id}")>

Please help me.