Can not pass value from Ajax to controller - ajax

My AJAX:
$.ajax({
url: '{{ URL::to('dashboard') }}',
type: 'GET',
data: { cid: val },
dataType:'JSON',
success: function(result) {
},
error: function(){
$('#status-msg').addClass('alert alert-danger');
$('#status-msg').text('Fejl!!');
}
});
My Route:
My Controller:
public function dashboard(){
if (Request::ajax()){
$cid = Input::get('cid');
var_dump(json_encode($cid));
} else {
echo "XX";
}
}
The cid is not passing to controller, the request AJAX is not working.

have you try change your ajax type to POST ?
$.ajax({
url: '{{ URL::to("yourURL") }}',
type:'POST',
data:"cid="+$(this).val()+"&_token=" + $("input[name=_token]").val(),
dataType:'JSON',
success:function(result){
},
error: function() {
}
});
Edit:
try to pass _token if you use {{Form::open}}.
hope this will help.

Try this:
$.ajax({
url: '<?php echo base_url() ?>index.php/contorller_name/method_name',
type: 'POST',
data: { cid: val },
dataType:'JSON',
success: function(result) {},
error: function() {
$('#status-msg').addClass('alert alert-danger');
$('#status-msg').text('Fejl!!');
}
});

Related

laravel send json object by ajax

$.ajax({
url: '{{ route('add.orders') }}',
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
data: JSON.stringify({
myArr : myArr
}),
cache: false,
type: 'POST',
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(data) {
alert(data);
}
})
my controller:
public function addOrders(Request $request) {
return $request->myArr;
}
I'm getting this one:
this is myArr:
How can i reach the object? Why it seems [object Object]
Can i reach myArr[0].id
İf you help me i will be glad, thank you.
You have to use like below
for(var i=0;i<data.length;i++){
console.log(data[i].id);
console.log(data[i].food_title);
}
$.ajax({
url: "{{ route('add.orders') }}",
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
data:{ myData: JSON.stringify(myArr),_token:"{{ csrf_token() }}" },
cache: false,
type: 'POST',
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(data) {
console.log(data);
}
});
#At Controller
public function addOrders(Request $request) {
dd($request->all());
}

Bad Request in AJAX

I am trying to develop a WordPress plugin. I am getting Bad Request from below AJAX code.
// On change division
$('body').on( 'change', '.division', function() {
$.ajax({
type: "POST",
url:localData.statesurl,
data:{
division: $(this).val(),
action: 'division_to_district_ajax'
},
success:function(rss){
alert(rss);
$('.district').empty();
var $opt = '';
$.each( JSON.parse(rss), function(key, value) {
$opt += '<option value="'+key+'">'+value+'</option>';
});
$('.district').append($opt);
},
error: function (jqXHR, textStatus, errorThrown) { alert(errorThrown); },
dataType: "json",
contentType: "application/json"
});
});
Could anyone say why I am getting Bad Request ?
Try like this
add_action( 'wp_ajax_division_to_district_ajax', 'division_to_district_ajax_function');
add_action( 'wp_ajax_nopriv_division_to_district_ajax', 'division_to_district_ajax_function');
function division_to_district_ajax_function(){
$myArr = array(
'response' => 'xyz'
);
$myJSON = json_encode($myArr);
echo $myJSON;
die();
}
// On change division
jQuery('body').on( 'change', '.division', function() {
jQuery.ajax({
url: '<?php echo admin_url( 'admin-ajax.php');?>',
type: "POST",
data: {'action': 'division_to_district_ajax', division: jQuery(this).val()},
cache: false,
dataType: 'json',
beforeSend: function(){
},
complete: function(){
},
success: function (response) {
console.log($response);
}
});
});

AJAX Post request in laravel 5.4

Have anyone got issue in ajax post request in laravel 5.4. I am unable to get request data in controller.
Ajax request is something like this:
$.ajax({
data: { 'selected_data':[2,4,5] },
type: "POST",
url: "{{ url('test') }}",
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
success: function (res) {
console.log(res)
},
});
In controller method, i am just doing
dd($request->all());
But getting empty array. can anyone help me with this issue?
Thanks!
Instead of this:
$.ajax({
data: { 'selected_data':[2,4,5] },
type: "POST",
url: "{{ url('test') }}",
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
success: function (res) {
console.log(res)
},
});
Try this:
$.ajax({
data: { selected_data:[2,4,5], _token: "{{csrf_token()}}" },
type: "POST",
url: "{{ url('test') }}",
success: function (res) {
console.log(res)
},
});

Typeahead.js Get ID

sorry but i don't find reply..
My problem is i can't retrieve the id of the selected product. The value = 0
My code :
$("#recherche").typeahead({
onSelect: function(item) {
alert(item.value); // = 0
},
ajax: {
url: "/personne/autocompletation",
displayField: "nomComplet",
triggerLength: 1,
method: "POST",
dataType: "JSON",
preDispatch: function (query) {
return {
query: query
}
}
},
});
My code HTML
<li class="active" data-value="0"><strong>C</strong>alloway</li>
Sorry for my english...
Try this
$("#recherche").typeahead({
source: function(query, process){
$.ajax({
url: "/personne/autocompletation",
displayField: "nomComplet",
triggerLength: 1,
method: "POST",
dataType: "JSON",
success: function (data) {
console.log(data);
}
});
}
});

An Array is no longer an array after an AJAX Sending

I use the jQuery UI sortable plugin and I don't succeed to send the "order" var (an array) with AJAX. After the ajax sending "order" is not an array anymore. I would like to have a solution only for the ajax sending.
$("#list-cat").sortable({
placeholder: 'highlight',
update: function() {
var order = $('#list-cat').sortable('serialize');
$.ajax({
dataType: 'json',
type: "POST",
url: "my_url.php",
data: { action: "edit_cat_order", id_member: "<?php echo $id_member; ?>", id_page: "<?php echo $id_page; ?>", order: order },
success: function(data){
if (data.a == true){
$.colorbox({ html: data.b });
}
},
error:function (xhr, ajaxOptions, thrownError){
alert(xhr.responseText);
}
});
}
});
In "my_url.php", a foreach loop doesn't recognize $_POST['order'] as an array : "Invalid argument supplied for foreach() ...". I think the problem is in the "data" line because $_POST['order'] is an array when I use :
$.post("my_url.php", order);
you can try
$.post("imy_url.php", {order:order});
EDIT
you can use $.makeArray
$.ajax({
dataType: 'json',
type: "POST",
url: "my_url.php",
data: { action: "edit_cat_order", id_member: "<?php echo $id_member; ?>", id_page: "<?php echo $id_page; ?>", order: $.makeArray(order)},
success: function(data){
if (data.a == true){
$.colorbox({ html: data.b });
}
},

Resources