Can't call ajax POST request inside FB.api - php

For Facebook login I'm using this code
FB.api('/me', {fields: 'birthday,cover,devices,email,first_name,gender,id,last_name,link,location,name,name_format,timezone,verified,website,locale'}, function(response) {
$.ajax({
url: '/login/facebook',
type: 'POST',
data: { fb: response, window: window.ui},
dataType: 'json',
success: function (data) {
console.log(data);
},
error: function (xhr, ajaxOptions, thrownError){
notice(xhr);
notice(ajaxOptions);
}
});
});
If i'm calling without {fields: '...'} its working but when added fields ajax sending GET request to server instead of post, how to get response with desired fields from FB.API and post it to server?

I fixed this problem with little change in code instead of url: '/login/facebook', I wrote url: '/login/facebook/', just / at the end and problem solved !

Did you try?
FB.api('/me', 'post', {fields: 'birthday,cover,devices,email,first_name,gender,id,last_name,link,location,name,name_format,timezone,verified,website,locale'}, function(response) {
$.ajax({
url: '/login/facebook',
type: 'POST',
data: { fb: response, window: window.ui},
dataType: 'json',
success: function (data) {
console.log(data);
},
error: function (xhr, ajaxOptions, thrownError){
notice(xhr);
notice(ajaxOptions);
}
});
});

Related

JQuery ajax post is empty on php remote server

When I run an ajax POST request on my localhost php server, $_POST array is filled. On a remote server the $_POST array is empty if I use google chrome, MS Edge; but $_POST is not empty on firefox.
When I investigated the HTTP Remote Address headers I noticed the following differences
in chrome --- Remote Address: 95.168.185.183:8080
in firefox ---Remote address:185.27.134.216:80
and if change the ajax call to type get, the code works well.
My ajax call looks like this:
$.ajax({
url: 'index.php/designer/ajax_add_template_to_cart',
type: 'post',
datatype:'json',
data:{
template_name : template_name,
size:size,
qty: 1
},
error: function(data) {
console.log(data);
},
success: function(data){
console.log(data);
},
});
Can you try by doing code sequence change in AJax request.
$.ajax({
url: 'index.php/designer/ajax_add_template_to_cart',
type: 'POST',
data:{
template_name : template_name,
size:size,
qty: 1
},
dataType:"JSON",
success: function(data){
console.log(data);
},
error: function(data) {
console.log(data);
},
});
//You must use valid URL
$.ajax({
url: 'index.php/designer/ajax_add_template_to_cart',
type: 'POST',
data: jQuery.param({ template_name: template_name, size: template_name ,qty:1}) ,
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
success: function (response) {
alert(response.status);
},
error: function () {
alert("error");
}
});

laravel 5.6 default login with ajax gives 422 Unprocessable Entity

I am using laravel 5.6 and I am trying to login with ajax. I have set all the things =>csrf ajax setup and also passing token, but I am still getting this when all credentials are right:
THis is my ajax code
$(document).ready(function () {
$('#sign-in-form').submit(function(ev) {
ev.preventDefault();
formData = $('#sign-in-form').serialize();
$.ajax({
url: '{{route('login')}}',
type: 'POST',
data: formData,
dataType: 'JSON',
beforeSend: function () {
// element.value='Processing...';
},
success: function (result, status, xhr) {
console.log(result);
},
error: function (xhr, status, error) {
alert(error);
}
});
})
});
There is nothing wrong with your codes. The message is the result of validations. To solve your issue you may need to enter at least 6 characters or more for your password and that should work.
Hope that helps.

return a json value from an mvc controller method in php

i would like to get an json type value from an mvc controller method. everything is correct but an error occures'.
my jquery ajax function:
function user_login(uname,pass){
$.ajax({
url: 'http://localhost/s/login_request',
type:'POST',
data:{uname:uname,pass:pass},
dataType:"json",
cache: false,
})
.done(function(response){
//do something
alert('1234');
})
.fail(function(jqXHR,textStatus){
alert(JSON.stringify(jqXHR));
});
}
and here is my php code(mvc controller method):
function login_request(){
header('Content-Type: application/json');
echo json_encode(array('testvalue'));
}
when i run the code, the .fail section executed and the following value was returned:
{"readyState":4,"responseText":"[\"testvalue\"]","status":200,"statusText":"OK"}
how can i solve this? thanks...
Try using
$.ajax({
url: 'http://localhost/s/login_request',
type:'POST',
data:{uname:uname,pass:pass},
dataType:"json",
cache: false,
success: function(data) { },
fail: function(xhr, status) { alert(JSON.stringify(xhr)) },
error: function() { },
complete: function() { alert('1234') }
});

how to add header while posting a url and raw data in php

i have to pass header information while posting a url and want to pass raw data in the url can any on help me on this
i have tried
function test(){
alert("test");
$.ajax({
type:"POST",
beforeSend: function (request)
{
request.setRequestHeader("X-APIKEY", "y5q9q1at8u-1uf4bao2yq-bsjdj3gh1g-u9ymh1t2f8-tt85pn4r50");
},
url: "https://backoffice.hostcontrol.com/api/v1/domain-is",
data: {"domain": "mahrosh.com"},
processData: false,
success: function(msg) {
alert(msg);
$("#results").append("The result =" + StringifyPretty(msg));
}
});
}
its not returning me results
You can add header variables as per given in below described code
function test(){
alert("test");
$.ajax({
type:"POST",
headers: { 'X-APIKEY': 'y5q9q1at8u-1uf4bao2yq-bsjdj3gh1g-u9ymh1t2f8-tt85pn4r50'},
url: "https://backoffice.hostcontrol.com/api/v1/domain-is",
data: {"domain": "mahrosh.com"},
processData: false,
success: function(data) {
alert(JSON.stringify(data));
},
error: function(data){
alert(JSON.stringify(data));
}
});
}
If server gives error in response than you have idea about it so i added the error section as well ...

jQuery posting to an external php file not working

I am not sure why but the post to my external PHP file is not working. The post request is not being received by the PHP file as nothing is being outputted.
Here's my jQUery;
$.post("AJAX/get_track_info.php", { url: "uploads/19c9aa51c821952c81be46ca9b2e9056.mp3"}, function(info){
$('#loadInfo').html(info);
});
And in the PHP file is just a
$trackurl = $_POST['url'];
Try something more like this
$.ajax({
type: 'POST',
url: 'AJAX/get_track_info.php',
data: { url: "uploads/19c9aa51c821952c81be46ca9b2e9056.mp3" },
dataType: 'text',
success: function (data, textStatus, jqXHR) { },
error: function (data, textStatus, errorThrown) { }
});

Categories