i am making a facebook like kind of thing and i am using ajax for this in which in ajax data the id of the post or photo will pass and it will be of type post and in route will go to controller for updation but for just testing now in route it will return any message or the id
the ajax part(javascript)
$.ajax({
url:"vote",
type:"post",
dataType:"text",
data:{id:x},
headers: {
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
},
success:function(data){
console.log(data+"sucees");
}
the php part(route part in web.php)
Route::post('/vote',function(){
if(Request::ajax()){
return Request::id();
}else{
Return "fail";
}
});
the error i am getting in console log
POST http://localhost:8000/vote 500 (Internal Server Error)
but whenever i am doing this
Route::post('/vote',function(){
if(Request::ajax()){
return Request::id();
}
it is correctly returing the data in json format
console log
{"id":"17"}sucees
help me anyone.
Related
I wanted to submit a for using ajax call in laravel 5.
In view i wrote something like
$("#updateSubmit").on('submit',function(e){
e.preventDefault();
var csrfToken = $('meta[name="csrf-token"]').attr("content");
$.ajax({
method:'POST',
url: '/account/updateForm',
//dataType: 'json',
data: {accountId:'1111', _token: '{{csrf_token()}}'},
success: function( data )
{
alert(data)
return false;
}
},
error: function(error ){
alert("There is some error");
}
});
and on controller side
public function update(Request $data )
{
return Response()->json(['success' => true],200);
}
while in route for post method
Route::post('account/updateForm', 'AccountController#update')->name('account/updateForm');
its working till ajax. on Submission of ajax it goes to controller action.
but it does not retrun back as ajax comes back in normal form submisson.
it just go to controller and stops there with {"success":true} line.
I want ajax to come back to view form so that I can perform different dependent actions.
Do you mean that when you submit your form, you just have a white page with {"success": true} ?
If that's the case, maybe the error is on your javascript.
Maybe your jQuery selector is wrong, or maybe your js isn't compiled ?
I am working on a Post Ajax request Function. Where the function takes some data and send it through an enabled CSRF_token post Request to a controller and then after evaluations on controller a message sent back to the view. but it seems i miss a small thing in my code.
My controller
public function PostMessage(Request $request){
$message=$request->someData; //getting data from request variable
return response()->json($message);
}
My jquery Ajax request function
$('.SendAjaxPostRequest').on('click', function() {
var value=$('.MessageHolder').val();
$.ajax({
method: 'POST',
url:'{{route('SVCate')}}', //SVCate is my route to the controller
dataType: 'JSON',
data: {_token:token,'someData':value,}
// #token gets it's value from a local view javaScrip Variable
})
.done(function (data) {
console.log(data);
})
});
My route function
Route::post('SendMessage','NessageController#PostMessage')->name('SVCate');
Check your apostrophes in the url. You are ending the string and beginning the string around SVCate change it to url:"{{route('SVCate')}}" to make sure that SVCate stays a string and does not break your string.
I have to pass parameters per post with ajax but not working on the console I get this:
POST http://localhost:8000/prueba2 405 (Method Not Allowed)
This is my routing:
Route::get('prueba2', 'HomeController#index');
This is my ajax:
$.ajax({
url: '{{url('prueba2')}}',
type: 'POST', // Send post data
data: 'type=fetch',
async: false,
success: function(s){
json_events = s;
}
});
This is my controller:
public function index(){
return 'hola';
}
All this is a test and is not the final driver nor the final ajax, but it seems to be some response by the controller. But unfortunately I get a 405.
If someone can help me with this serious problem it would be a lot of help
You are receiving a MethodNotAllowedException because you defined a GET route with Route::get('prueba2', 'HomeController#index');, but you do a POST request.
Change your AJAX type to GET or use Route::post().
The last one would look like:
Route::post('prueba2', 'HomeController#index');
Im making a Login whit facebook.
using the javascript sdk im geting the response (i guess is JSON), and i want to send this response to a php file to check if the user is in the database or not.
so heres what i got so far.
this is the function i call when the user is loged into facebook.
function testing(){
FB.api('/me', function(response) {
response = JSON.stringify(response);
//call another function, sending the data recived
ajaxlog(response);
});
}
and here is the ajaxlog function
function ajaxlog(facedatos){
$.ajax({
type: "POST",
url: "facebook-ajax-login.php",
dataType: "json",
data: facedatos,
success: function(response){
//the php brings the conect response true or false
if(response.conect==true){
$("#exist").html(response.data);
}else{
}
},
beforeSend: function(){
$("#exist").html("<img class='img-responsive ajax-l' style='width:40px;padding-top:10px;margin-right:10px;' src='images/ajax-loader.gif' />")
}
});//<!--ajax-->
im doing alerts and the facebook data comes with no problem. i think the issue is how i send the data by post, im not reciving the data in the php
I find the issue by myself,
the problem is that i was sending the post request whitout a name.
in the ajax function changed
data: facedatos,
for
data:{
face: facedatos
}
and in the php recived the data as $_POST["face"];
It is working right now , but I have some feedback of user saying that the facebook popup is blocked by the browser
So what I am doing right now: I have a form that is being validated via ajax (making a call to a php page) , then if the response is successful, it ask for the user login/permissions. I assume that the popup is sometime blocked because the browser consider the ajax response not as an user action.
So my code looks like this :
$("#submit").click(function (event) {
event.preventDefault();
$.ajax({
url: url,
type: type,
data: form_data,
success: function(result){
if(result==""){
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
FB.api('/me/permissions', function (response) { ... });
} else if (response.status === 'not_authorized') {
FB.login(function (response) { ... });
}
}
}
}
});
Any idea other than putting the facebook calls before the form validation?
You can make ajax request as synchronous call. I don't like it though
btw, what kind of validation you are doing?