Getting 400 Bad Request Response from Facebook Graph API using jQuery.ajax - ajax

I want to send a message whenever a button is clicked, but I'm getting 400 Bad Request Errors. It works fine when I do it in Postman.
Here's my jQuery.ajax code:
$.ajax({
method: "POST",
url: 'https://graph.facebook.com/v14.0/me/messages?access_token=<MY_ACCESS_TOKEN>',
data: {
"recipient": {
"id": psid
},
"message": {
"text": "hello, world!"
}
},
contentType: "application/json",
success: function (response) {
console.log(response)
}
})

Related

{"readyState":4,"status":200,"statusText":"load"} parsererror ajax local

I am practising my ajax with a json file on local computer, using google chrome. the json file is par of sources when I inspect the website but i keep getting a parsererror
hears my js code
$(function(){
$.ajax({
type: 'GET',
url: 'sample3.json',
dataType: 'jsonp',
crossDomain: true,
cache: false,
async: true,
jsonp: false,
success: function(data) {
console.log('success', data);
},
error:function(e, msg){
console.log('error')
console.log(JSON.stringify(e) + " " + msg);
}
})
})
I keep getting {"readyState":4,"status":200,"statusText":"load"} parsererror
my json file
[
{
"id": 1,
"name": "Will",
"drink": "Americano w/ Creme"
},
{
"id": 2,
"name": "Laura",
"drink": "Vanilla Macchiato"
}
]
to see what I am trying to create
https://jsfiddle.net/Anaconda2468642/rskfwucp/3/
and I am also getting this error:
error Error: myCallback was not called
at Function.error (jquery.min.js:2:2616)
at e.converters.script json (jquery.min.js:2:84147)
at jquery.min.js:2:79470
at l (jquery.min.js:2:79587)
at HTMLScriptElement.i (jquery.min.js:2:83436)
at HTMLScriptElement.dispatch (jquery.min.js:2:43064)
at HTMLScriptElement.v.handle (jquery.min.js:2:41048)

My request to an API doesn't work with ajax (err:500), despite working with postman

I need some help to use an API (Easy!Appointment).
I try to use some ajax code to create a new customer profile from my site into the API database, but I receive an error 500.
I send the same request by postman and it worked.
I already use a GET type request with ajax to display some info from the datadase into my site view and it worked juste fine too.
I have only problem with the POST request, from my site.
Can you help me to make it work ? Thanks ! :-)
Here the ajax code who doesn't work :
$.ajax({
type: "POST",
url: "http://localhost/easyappointment-test/index.php/api/v1/customers",
dataType: 'json',
data: {
"firstName": "John",
"lastName": "Doe",
"email": "jo...#doe2.com",
"phone": "0123456789"
},
headers: {"Authorization": 'Bearer dbhf6da4ew7nw79bhvaays',
"Content-Type" :'application/json'},
success: function (data){
console.log(data);
},
error: function (err){
console.log(err);
}
});
Thanks for your help ! :-)
PS : Sorry for my poor english !
stringify data before sending
$.ajax({
type: "POST",
url: "http://localhost/easyappointment-test/index.php/api/v1/customers",
dataType: 'json',
data: JSON.stringify({
"firstName": "John",
"lastName": "Doe",
"email": "jo...#doe2.com",
"phone": "0123456789"
}),
headers: {"Authorization": 'Bearer dbhf6da4ew7nw79bhvaays',
"Content-Type" :'application/json'},
success: function (data){
console.log(data);
},
error: function (err){
console.log(err);
}
});

How to properly send an Ajax Post request for creating fusion tables using google OAuth2?

I am trying to create a fusion table using the documentation provided by Google fusion table REST API https://developers.google.com/fusiontables/docs/v2/using.
I am sending an ajax request in the following format
$.ajax({
type: "POST",
crossDomain: true,
contentType: "application/json",
url: "https://www.googleapis.com/fusiontables/v2/tables",
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", token);
},
data: {
access_token: token,
"name": "Insects",
"columns": [
{
"name": "Species",
"type": "STRING"
},
{
"name": "Elevation",
"type": "NUMBER"
},
{
"name": "Year",
"type": "DATETIME"
}
],
"description": "Insect Tracking Information.",
"isExportable": true
},
async: false,
success: function(result){
alert(result);
},
error: function(result) {
alert('error');
}
});
But I am getting a 401 error. I tried setting Bearer , token in the authorization header but still no success.Any pointers will be much appreciated.Thank you
I solved my issue with:
xhr.setRequestHeader("Authorization", "Bearer "+ token);

How to update snippet and status values of already uploaded video through YouTube API v3?

How to change the snippet and status values of an already uploaded video through YouTube API v3 "https://www.googleapis.com/youtube/v3/videos" using AJAX request?
My UPDATED CODE (NOT Working):
$.ajax({
type: "PUT",
dataType: "jsonp",
url: "https://www.googleapis.com/youtube/v3/videos?part=id,snippet,status&key=<API Key>",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + <Access Token>
},
data: $.parseJSON('{"id": "<Video ID>", "snippet": {"title": "New Title", "description": "New Description"}, "status": {"privacyStatus": "unlisted"}}'),
success: function(data, error, completeError) {
alert( JSON.stringify(data));
alert( JSON.stringify(error));
alert( JSON.stringify(completeError));
}
});
Make a PUT request to the videos/update endpoint with the parameter part=id,snippet,status:
HTTP PUT: https://www.googleapis.com/youtube/v3/videos?part=id,snippet,status&key={YOUR_API_KEY}
Body of your request:
{
"id": "VIDEO_ID",
"snippet": { // any snippet fields you want to change
"description": "New Video Description"
},
"status": { // any status fields you want to change
"privacyStatus": "public"
}
}
Here is the code I used when having a play around. It works well though.
It doesn't use JQuery but should point you in the right direction.
function UpdateVideoInfo(video_id){
var resource = {
'snippet':{
'title' : 'test title',
'description' : 'test description',
'categoryId' : 22
},
'status' : {
'privacyStatus' : 'private'
},
'id': video_id
};
post_string = JSON.stringify(resource);
var ajax = new XMLHttpRequest();
ajax.open('PUT', 'https://www.googleapis.com/youtube/v3/videos?part=snippet,status', true);
ajax.setRequestHeader("Authorization", '<?php echo $authorization_header; ?>');
ajax.setRequestHeader("Content-type", "application/json; charset=UTF-8");
ajax.send(post_string);
ajax.onload = function() {
if (ajax.status == 200) {
alert(ajax.responseText);
}
};
}

Ajax Post Create Features Google Maps Engine API

So here is my code, i've already made the oauth handshake, and that has given me the authentication token which i'm including in my header. I am simply trying to batch insert some features into an existing table. I keep getting a 400 "parseError" - This API does not support parsing form-encoded input.
Here's some of my code. I have no idea where i'm derping any ideas.
$(document).ready(function(){
$('.pickTrip').bind('click', function(){
var pointData = {
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-87.397980,
44.795067
]
},
"properties": {
"gx_id": "1242342",
"name": "Jimmy Choo",
"phone": "(920)555-4242",
"email": "jchoo#aol.com",
"vehicle": "mazda, b2300, 1994"
}
}
]
};
console.log(pointData);
var url = "https://www.googleapis.com/mapsengine/v1/tables/{table id}/features/batchInsert";
jQuery.ajax({
type: "POST",
url: url,
data: pointData,
dataType: 'application/json',
headers: {
'Authorization': 'Bearer ' + authResult.access_token
},
success: function(response) {
// Log the details of the Map.
console.log(response);
},
error: function(response) {
response = JSON.parse(response.responseText);
console.log("Error: ", response);
}
});
});
});
jQuery takes the object provided in the 'data' parameter and turns the key/value pairs into an encoded query string. You will want to send the raw JS object and make sure it's not marked as form encoded. To do that, change the 'dataType' parameter to 'contentType' and update the data value to take the "stringified" version of the JSON object.
Like this:
data: JSON.stringify(pointData),
contentType: 'application/json',

Resources