Why isn't jQuery Post sending any data? - javascript

I am trying to use Instafeed to show my Instagram feed on my website. However, I do not want to run it everytime someone accesses my website, so I'm trying to write the response to a file. For that I am using the following code
var userFeed = new Instafeed({
get: 'user',
userId: 'xxx',
accessToken: 'xxx',
sortBy: 'most-recent',
limit: '12',
template: '<img src="{{image}}" title="{{caption}}"/>',
link: 'true',
success: function(response) {
console.log(response.data);
$.post('saveInstagram.php', {list: response.data}, function(o) {
console.log(o);
}, 'json');
}
});
userFeed.run();
saveInstagram.php (very basic, just to test)
$list = $_POST['list'];
$myfile = fopen("instagramFeed.html", "w") or die("Unable to open file!");
fwrite($myfile, $list);
fclose($myfile);
The console log shows the correct data which should get sent to saveInstagram.php. The response isn't being sent however. I tried changing the value manually to {list: 'test'} in order to check the save-script, and that works, 'test' is being written to the file. So I don't seem to understand why response.data isn't being sent by the script, but does show up in the console.log. Thank you in advance!

Related

FilePond,Laravel restore uploaded temporary files on validation failure

after searching a lot here and there I am going to put my question here . If any body can help out in this regard . Let me explain the things I am working on Laravel and Filepond .
Filepond upload and revert is working perfectly but I am facing problem in restoring the file back if the validation gets failed i-e restore the file on filepond.
files: [{
source: 'filename',
options: {
type: 'limbo',
},
}, ],
the source is coming from laravel controller function
FilePond.setOptions({
server: {
process: './filepond-upload',
revert: './filepond-delete',
restore: './filepond-restore',
// restore: {
// url :'./filepond-restore/?id=',
// method :'GET',
// },
headers: {
'X-CSRF-TOKEN': '{{ csrf_token() }}',
'Access-Control-Expose-Headers': 'Content-Disposition,'
// 'Access-Control-Expose-Headers': 'Content-Disposition',
}
}
});
Controller function -
public function filepondRestore(Request $request, string $id) {
$abc = ('/posts/tmp/post6399a6ba2ea280.18814893/presentation_1.png');
return response()->json('', 200, [
'Content-Type' => 'image/png',
'Content-Disposition' => 'inline;
filename="'.$abc.'"',
]);
}
but either get 302 redirection or 500 server error.
If any body have implemented such kind of functionality I ll be thankful for sharing.
Thanks in advance.
Happy Coding

Why does axios always send empty object?

I'm trying to figure this out and it's driving me mad. I am trying to send an object of data using an axios post request. It goes to the file okay but the object is always empty. So when I use this code:
axios.post('php/send_email.php', {
params: {
name: 'niall'
}
})
.then(function (result) {
console.log(result)
});
And then use the php below:
<?php
echo $_POST['name'];
?>
It will always output an error of name being undefined for the result from the http request.Can anyone shed some light on this and where I am going wrong?
Also I noticed that this seems to be a problem with sending an object because when I try:
axios.post('php/send_email.php', 'niall' )
.then(function (result) {
console.log(result)
});
And then print out the array using:
<?php
print_r($_POST);
?>
It will show:
Object {data: "Array↵(↵ [niall] => ↵)↵", status: 200, statusText: "OK", headers: Object, config: Object…}
Try sending like this
axios.post('php/send_email.php', { name: 'niall' }})
instead of wrapping parameters in an extra params object.

Showing kartik growl via ajax in yii2

Am using kartik growl and i would like to show the growl via ajax success
I have tried
This is the javascript code:
$.post({
url: "forwardpr", // your controller action
dataType: 'json',
data: {keylist: keys,user:userdata},
success: function(data) {
console.log(data);
//$.pjax.reload({container:'#forward-grid'});
$.growl( data.growl );
},
error: function(err){
alert(err);
console.log("server error");
}
});
This is the controller code:
$growl = [
'title' => "Group members updated.<hr>",
'icon' => 'glyphicon glyphicon-ok-sign',
'message' => "Successifully updated.",
'showSeparator' => true,
];
echo json_encode(['response'=>"Successifully forwarded pr(s)", 'growl' => $growl ]);
If you see TypeError: $.growl is not a function, then it means you have not included required files to AppAsset.php file.
To solve this problem, go to assets/AppAsset.php file and add:
public $css = [
// ... Something else might be here
'css/jquery.growl.css',
];
And
public $js = [
// Something else might be here
'js/core.js',
];
Because of missing .js file, you have that error in console (TypeError: $.growl is not a function). But you also must add .css file as well because without it you will not see growl, even though it works.
I believe you're using the wrong function. Here's offical docs:
"Another important update is since version 3.x you no longer call the
plugin using $.growl(...) you must use $.notify(...)."
In another words, just try using $.notify(...) instead of $.growl(...).

Triggering Javascript Code from PHP Laravel Controller

I'm using OAuth for login in my Laravel Controller. Its working fine but the thing is when the user is registered for the first time, I wanna trigger the HTML 5 geolocation API to fetch the user's current location and do some mixpanel stuff. Earlier I was using AJAX in the JS for the login so there was no such problem but now that I've implemented a complete server side solution, I'm stuck with this one problem.
The Laravel Controller code looks something like this :
function callback(){
\\ fetch the access token and graph data
if($res = \Auth::mjAuthenticate('facebook', $fbData)){
$user = \Auth::scope()->getUser();
return \Redirect::to('events');
}
if (\Auth::mjRegister('facebook', $fbData)) {
$user = \Auth::scope()->getUser();
return \Redirect::to('events');
}
return $this->handleFailure('Some Problem Occured');
}
The Earlier JS Code was :
ajax
.post('auth/login', {
data: {
oauth_provider: 'facebook',
oauth_token: accessToken
},
cache: false
})
.done(function(data) {
mixpanel.track('User Logged In', {
id: data.resource.id,
provider: 'Facebook',
email: data.resource.email,
first_name: data.resource.first_name,
last_name: data.resource.last_name
});
if (data.msg == 'Resource registered') {
if(navigator.geolocation){
// Prompt for Allow Deny Geolocation popup.
}
}
});

Error handling for ajax call from jquery jtable

I have a jtable that has a listAction that calls an action in the controller to return the data for the table. If the user refreshes the page while the action is in progress its alerts:
Here is the jtable:
$('#cuserTable').jtable({
title: 'Users',
paging: true,
pageSize: 15,
sorting: true,
ajaxSettings: {
contentType: 'application/json'
},
actions: {
listAction: '#Url.Action("LoadUserTable")'
},
.
.
How and where can I add an error handler to display a custom alert (or not even display an alert) if the user refreshes during a table load/reload?
you can handle the error messages for the dialog:
Imagine you are performing an update and you want to return to the dialog message the error occurred:
try{
//UPDATING
[...]
Your update code goes here
[...]
$jTableResult = array();
$jTableResult['Result'] = "OK";
echo json_encoded($jTableResult);
}catch(Exception $ex){
$jTableResult = array();
$jTableResult['Result'] = "NOK";
$jTableResult['Message'] = "Error while updating the record XYZ";
echo json_encoded($jTableResult);
}
The response should have
data = {
Message : "error message",
Result : "ERROR"
};
$dfd.resolve(data);
In jquery jtable, you have to do $dfd.resolve(data)

Categories

Resources