jsonp return number - arrays

Hi in my succes function im trying to return 6 numbers from my callback jsonp function and pass to a var, something like; im out of ideas thnx P
for (var bw=0; bw < bw_numbers.length; x++) {
$('#_pnl' + bw).innerHTML = bw_numbers[bw];
}
jsonCallback(
   {
"bw_numbers": [10, 12, 15, 24, 27, 41]
}
);
var url = 'http://www.blabla.com/ajax/bw_results_latest.json?callback=?';
$.ajax({
type: 'GET',
url: url,
async: false,
jsonpCallback: 'jsonCallback',
contentType: "application/json",
dataType: 'jsonp',
success: function(json) {
//do my array thing!!!!
},
error: function(e) {
alert(e.message);
}
});

you need to write the jsonCallBack function
$.ajax({
type: 'GET',
url: url,
async: false,
contentType: "application/json",
dataType: 'jsonp',
error: function(e) {
alert(e.message);
}
});
function jsonCallBack(data)
{ alert(data); }
If you return like
jsonCallback([10, 12, 15, 24, 27, 41])
you can access it as array from javascript

I got it now, I wasnt sure if the json data was able to be used as an array, it is and here is code! Thanks P
var url = 'http://www.blabla.com/ajax/results_latest.json?callback=?';
$.ajax({
type: 'GET',
url: url,
processData: true,
async: false,
jsonpCallback: 'jsonCallback',
contentType: "application/json",
dataType: 'jsonp',
success: function (data) {
processData(data);
}
});
function processData(data){
for (var x=0; x < data.bw_numbers.length; x++) {
document.getElementById('_pnl' + x).innerHTML = data.bw_numbers[x];
}
}

Related

How to receive formData in laravel? [duplicate]

This question already has answers here:
How to make POST requests with the FormData API
(2 answers)
Closed 3 years ago.
I've seen many posts similar to this questions, but I can't find the solution.
This is the request in frontend(angularjs).
formData = new FormData();
let blob = dataURLtoBlob(canvas.toDataURL('image/png'));
console.log("blob", blob);
formData.append("cropped_image[]", blob);
formData.append("title", $('#title').val());
console.log($('#title').val());
console.log(formData.has('title'));
console.log(formData.has('cropped_image[]'));
console.log("formData", formData);
var req = {
method: 'POST',
url: '/admin/logos/save',
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
data: formData,
contentType: false,
cache: false,
processData: false
}
$http(req).then(function (json) {
console.log('success');
console.log(json.data);
location.href = '/admin/logos';
});
And I've received in laravel backend.
$logo->title = $request->title;
print_r('----------------------------------\n');
print_r($request->all());
count($request->all());
foreach ($request->all() as $x => $value){
print_r($x);
print_r('sldf');
print_r($value);
}
print('-------------');
// $logo->filename = $request->filename;
print_r($request->cropped_image);
I've used several ways to receive, but $request->all() returns empty array().
Please help me.
I've solved and the request should be as follows.
var req = {
method: 'POST',
url: '/admin/logos/save',
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
'Content-Type': undefined
},
transformRequest: angular.identity,
data: formData,
cache: false,
processData: false
}
You may add dataType: 'json' to your req variable
var req = {
method: 'POST',
dataType: 'json',
url: '/admin/logos/save',
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
data: formData,
contentType: false,
cache: false,
processData: false
}
Then in your controller
dd($request->input->all());

How to Generate ngResource url with two parameters

i'm trying to generate this url using ngResource in angularjs:
http://url_api/contact/58dc70f18e029b1338a97abc/address/58e3e3988e029a1aec5ad465
but instead i'm getting this:
http://url_api/contact/58dc70f18e029b1338a97abc/address?address_id=58e3e3988e029a1aec5ad465
This is my factory
factory('Addresses', function($resource) {
return $resource(url_base + 'contact/:id/address',{},{
get: {
method: 'GET',
isArray: true,
url: url_base + 'contact/:id/address'
},
save:{
method: 'POST'
},
delete:{
method: 'DELETE',
url: url_base + 'contact/:id/address/:addressId',
// params:{
// id: '#_id',
// addressId: '#_addressId'
// }
}
});
})
and this is how i call it later
Addresses.delete({id:$scope.id_contact,address_id:address_id_delete})
Please could anyone help me? I'm in a hurry
You need to mention your params in the empty object you have. Like this:
factory('Addresses', function($resource) {
return $resource(url_base + 'contact/:id/address',{
id: '#id',
addressId: '#addressId'
},{
get: {
method: 'GET',
isArray: true,
url: url_base + 'contact/:id/address'
},
save:{
method: 'POST'
},
delete:{
method: 'DELETE',
url: url_base + 'contact/:id/address/:addressId',
// params:{
// id: '#_id',
// addressId: '#_addressId'
// }
}
});
})
The parameters in the url template need to match parameters in the call:
delete:{
method: 'DELETE',
url: url_base + 'contact/:id/address/:addressId',
// params:{
// id: '#_id',
// addressId: '#_addressId'
// }
}
//WRONG address_id
//Addresses.delete({id:$scope.id_contact,address_id:address_id_delete})
//RIGHT addressId
Addresses.delete({id:$scope.id_contact,addressId:address_id_delete})

Angular resource custom post method

I need to post data to a SharePoint list, and I want to clean up my code by using
a resource factory, until now I have posted data like this:
this.save = function(data) {
data["__metadata"] = { "type": getItemTypeForListName('ListName') };
var restQueryUrl = appweburl + "/_api/lists/getByTitle('ListName')/items";
$.ajax({
url: restQueryUrl,
type: "POST",
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"content-Type": "application/json;odata=verbose"
},
data: JSON.stringify(data),
success: function (data) {
console.log(data);
},
error: function (error) {
alert(JSON.stringify(error));
}
});
};
And so far my resource factory looks like this:
myApp.factory('Entry', function($resource) {
return $resource(appweburl + "/_api/lists/getByTitle('ListName')/items", {}, {
get: {
method: 'GET',
headers: { "Accept": "application/json; odata=verbose" },
url: appweburl + "/_api/lists/getByTitle('ListName')/items?$select=Id,Title,Description&$filter=ID eq :ID"
},
query: {
method: 'GET',
headers: { "Accept": "application/json; odata=verbose" },
url: appweburl + "/_api/lists/getByTitle('ListName')/items?$select=Id,Title,Description"
}
})
});
How can I 'convert' my save function to a resource method?
Okey, so I was easier than I thought, what I had to do was run the function 'getItemTypeForListName' before calling the save function, this adds the metadata needed to save to sharepoint. And in my resource factory add this:
save: {
method: 'POST',
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"content-Type": "application/json;odata=verbose"
}
}
And then in my controller call it like this:
$scope.test = new Entry();
$scope.test.Title = 'testTitle';
// function to set metadata
$scope.test["__metadata"] = { "type": getItemTypeForListName('ListName') };
Entry.save($scope.test);

TypeError: Object #<Resource> has no method 'push'

I have searched other solutions but it doesn't work, solution was to put isArray: true, but it doent work. I am working with objects btw. I am getting this message in console, but everything works, data has been send to my backend api. How to remove that error ?
app.factory('Category', function($resource, $http) {
return $resource('http://localhost/api/v1/category/', {id: "#id"},
{
update: {
method: 'POST',
isArray: false
},
save: {
method: 'PUT'
},
query: {
method: 'GET',
params: {id: '#id'},
isArray: false
},
create: {
method: 'POST',
headers: {'Content-Type': 'application/json'}
},
drop: {
method: 'DELETE',
params: {id: '#id'}
}
}
);
});
Here is my function
$scope.createInventory = function(){
Inventory.create($scope.newinfo);
$scope.info.push(Inventory);
};
$scope.info not a array. You must specify $scope.info to array, then you can push any values to $scope.info.
Try this
$scope.info=[];
$scope.createInventory = function(){
Inventory.create($scope.newinfo);
$scope.info.push(Inventory);
};

ExtJs Add Json Data

I am facing problem in ExtJs. OnClick function it should load data for one time. But it is replicating the same data on every click.
Here is my code:
function onselectionchange()
{
jQuery.ajax({
type: "POST",
url: "/Maintainer/getfolders",
data: null,
contentType: "application/json; charset=utf-8",
async: true,
success: function (result) {
var store = Ext.getCmp('gridpanel').getStore();
store.loadData(result, true);
},
error: function (p_Result) {
var error = "Error on controller: \"" + p_Controller + "\" in function: \"" + p_ControllerFunction + "\" statusText: \"" + p_Result.statusText;
alert(error);
},
traditional: true
});
}
function onselectionchange()
{
if(clickonce == "")
{
ajax({
type: "POST",
url: "/url",
data: null,
contentType: "application/json; charset=utf-8",
async: true,
success: function (result) {
enter code here
clickonce = 1;
}
});
}
else
{
alert("already selected once");
}
}
What i'am doing here is creating a global variable with clickonce. When you first click on the button the json will get uploaded and the flag on this variable will set to 1 hence it won't be populated again.

Resources