Reputation: 277
I am creating a web site. In this web site , I have used a Rest API. Basically I send a JSON request using JavaScript and Ajax. Then I have created a HTML form to get inputs. Now I want to get numeric value from the form and I want to use that value as variable in the JSON request.
Here is HTML form.
<form class="form-horizontal" method="POST" action="#" enctype="multipart/form-data" id="signupForm">
<div class="col-md-5 col-md-offset-1">
<div class="form-group">
<label class="label-control">Origin City</label>
<input type="text" class="form-control" name="OriginLocation" value="CMB" id="OriginLocation">
</div>
</div>
<div class="col-md-2 col-md-offset-1">
<div class="form-group">
<label class="label-control">Adults</label>
<select name="adults" class="form-control" id="adults">
<option value="1">1</option>
<option value="2">2</option>
</select>
</div>
</div>
<div class="col-md-8 col-md-offset-2">
<center><a href="#" id="ghsubmitbtn" class="btn btn-success">Search Flight Data</a></center>
</div>
</form>
I want to use "adults" as a variable in this Quantity. Then I have tried like below. When I try like this , it sends request with quotes. But , I want to send this value qithout quotes.
How can I do this ??
Here is the JSON Request.
"TravelerInfoSummary": {
"SeatsRequested": [1],
"AirTravelerAvail": [{
"PassengerTypeQuantity": [{
"Code": "ADT", //adt cnn inf
"Quantity": adults // What I want - "Quantity": 1 and What I get "Quantity": "1"
},
{
"Code": "CNN",
"Quantity": 1
}
]
}]
}
Here is the JavaScript , Ajax that I used.
<script type="text/javascript">
$(document).ready(function(){
$('#ghsubmitbtn').on('click', function(e) {
var adults = $('#adults').val();
var JSONObj = {
"TravelerInfoSummary": {
"SeatsRequested": [1],
"AirTravelerAvail": [{
"PassengerTypeQuantity": [{
"Code": "ADT", //adt cnn inf
"Quantity": 1
},
{
"Code": "CNN",
"Quantity": 1
},
{
"Code": "INF",
"Quantity": 2
}
]
}]
}
};
var data = JSON.stringify(JSONObj);
$.ajax({
url: 'https://api-crt.cert.havail.sabre.com/v4.2.0/shop/flights?mode=live&limit=50&offset=1',
method: 'POST',
contentType:"application/json; charset=utf-8",
data: data,
headers: {"Authorization": 'Bearer ' + bat},
success: function (data) {
console.log(data);
for (var v = 0; v <= 999; v++) {
var row = $('<tr><td>' + data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].SequenceNumber + '</td><td>'
+ data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[0].FlightSegment[0].DepartureDateTime + '</td><td>'
$('#tblData').append(row);}
},
});
});
</script>
Upvotes: 0
Views: 1172
Reputation: 10163
value="1"
means the value you'll get from $('#adults').val()
will be string 1
. To turn it into integer 1
you use parseInt
.
Like this:
...
"Quantity": parseInt(adults, 10) // always supply radix 10 for safety
...
JSON.stringify
will now know the value of "Quantity"
is a Number and serialize it accordingly.
Upvotes: 1