I need to print data of my function below, so far i tried to put it in loop like $.each(data.data, function(key, value) {
, also tried getting results directly like data.price
or data['price']
even + price +
nothing works, return undefined
.
script
function myfunction(){
$.ajaxSetup({
headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') }
});
var cityname = $("#city option:selected").text();
cityname = cityname.substring(cityname.indexOf("-")+2);
var statename = $("#province option:selected").text();
if(statename && cityname) {
console.log(statename + " - " + cityname);
$.ajax({
url: '{{ url('getcityandstate') }}/' + statename + '/' + cityname,
type: "GET",
dataType: "json",
success:function(data) {
console.log(data);
// $('select[name="postchoose"]').append('<option id="postchoose" class="form-control" value="'+ price +'">'+ title +'</option>');
}
});
}
};
results
[…]
0: {…}
cart_amount: null
city: "Pandeglang"
created_at: "2018-03-23 12:19:28"
id: 6
price: "20"
state: "Banten"
title: "gfgg"
updated_at: "2018-03-23 12:24:47"
__proto__: Object { … }
length: 1
__proto__: Array []
Question
- How to print my data?
UPDATE
here is my controller
public function flatshippingcostincart(Request $request, $statename,$cityname) {
$state = $request->statename;
$city = $request->cityname;
if($state != '' && $city != ''){
$shipping = Shipping::where('state', '=', $state)->where('city', '=', $city)->get();
return response()->json($shipping);
}
}