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 });
}
},
Related
$.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());
}
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);
}
});
});
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);
}
});
}
});
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!!');
}
});
I used this code :
(function($){
var albumID = 'NNbeO';
var albumAPI = "https://api.imgur.com/3/album/" + albumID + "/images";
$.ajax({
url: albumAPI,
headers:{
'Authorization':'xxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
},
type: 'GET',
dataType: 'json',
success: function(data) {
alert(data.data[0].link);
},
error: function() { console.log("ERRORZ"); }
});
})(jQuery);
But I got this error :
{
"data": {
"error": "Malformed auth header",
"request": "\/3\/album\/NNbeO\/images",
"method": "GET"
},
"success": false,
"status": 403
}
I got my solution. It works fine now. Below is my working code. It's problem was, I've not added Client-ID text with Authorization headers.:
(function($){
var albumID = 'NNbeO';
var albumAPI = "https://api.imgur.com/3/album/" + albumID + "/images";
$.ajax({
url: albumAPI,
headers:{
'Authorization':'Client-ID xxxxxxxxxxxxx'
},
type: 'GET',
dataType: 'json',
success: function(data) {
alert(data.data[0].link);
},
error: function() { console.log("ERRORZ"); }
});
})(jQuery);