laravel send json object by ajax - 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());
}

Related

Kendo DataSource Update function Not Firing

I know this question has been asked with answers I have reviewed over and over. I still can not get this kendo dataSource update function to fire. It fires without the function(e) {}
update:
{
url: "/SettingsAdmin/UpdateGrid",
dataType: "json",
type: "POST",
//data: {
// result: kendo.stringify(result.data)
//},
},
That works. What doesn't work is when I try implementing the update as a function
update: function(e)
{
debugger
alert("update")
$.ajax({
url: "/SettingsAdmin/UpdateGrid",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
data: {
models: kendo.stringify(e.data.models)
},
success: function (result) {
alert(e.success(result));
},
error: function (result) {
alert(e.error(result))
e.error(result);
}
});
},
Here is the whole dataSource defined.
return new kendo.data.DataSource({
transport: {
read: {
url: url,
contentType: 'application/json; charset=utf-8',
type: "POST",
dataType: "json"
},
update:
{
url: "/SettingsAdmin/UpdateGrid",
dataType: "json",
type: "POST",
//data: {
// result: kendo.stringify(result.data)
//},
},
update: function(e)
{
debugger
alert("update")
$.ajax({
url: "/SettingsAdmin/UpdateGrid",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
data: {
models: kendo.stringify(e.data.models)
},
success: function (result) {
alert(e.success(result));
},
error: function (result) {
alert(e.error(result))
e.error(result);
}
});
},
parameterMap: function (data, operation) {
return JSON.stringify(data);
}
},
schema:
{
data: "result.data",
total: "result.total",
aggregates: "result.aggregates",
groups: "result.groups",
errors: "result.errors",
model:
{
id: "name",
//fields: {
// //id: { required: false, type: 'number', nullable: true },
// name: { editable: false, type: "string" },
// value: { editable: true, type: "string" }
//}
}
},
pageSize: 10,
batch: false,
serverPaging: true,
serverFiltering: true,
serverSorting: true,
});
All transport actions (read, update, create, destroy) must be defined in the same way which I wasn't doing. I had defined my read action as
read: {
url: url,
contentType: 'application/json; charset=utf-8',
type: "POST",
dataType: "json"
},
And then the update with a function
update: function(e)
{ }
Can't do that. I need to define my read: as a function

Ajax get response body data name with space

I am able to read specific data from responses with ajax GET example by using data.investing.
but one of my data name is unrest and war and i am unsure how do i write it inside my javascript, as i tried data.unrest and war and it didnt work.
$(document).ready(function(){
$("button").click(function(){
$.ajax({
type: 'GET',
url: "https://agile-bayou-24340.herokuapp.com/users/getTodayData",
contentType: 'application/json',
dataType: 'json',
responseType: 'application/json',
xhrFields: {
withCredentials: false
},
headers: {
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET',
'Access-Control-Allow-Headers': 'application/json',
},
success: function (data) {
console.log(data.investing[0].title);
$('#div1').html((data.investing[0].title));
},
error: function (error) {
console.log("Error");
}
});
});
});
this is my current code
You can parse Json to Array and use key's
success: function (data) {
data = $.parseJSON(data)
console.log(data['investing'][0]['title']);
$('#div1').html((data['investing'][0]['title']));
},

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)
},
});

Can not pass value from Ajax to controller

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!!');
}
});

KendoUI re-posts when delete is used

When new entry added into datagrid, and if page is not refreshed but some rows deleted,
tornado receives same added data as post, along with delete method
Why is that happening? I have differnt transport methods for each:
transport: {
read: {
url: '/api/notes/',
dataType: 'json',
type: 'GET',
},
create: {
url: '/api/notes/',
dataType: 'json',
type: 'POST'
},
update: {
url: '/api/notes/',
dataType: 'json',
type: 'PUT',
},
destroy: {
url: function(row) {
return '/api/notes/' + row.id;
},
type: 'DELETE',
},
ok, doing $("#grid").data("kendoGrid").dataSource.read(); on create.complete fixed it.

Resources