I can't upload videos to youTube with Javascript anymore. If I call the URL
$.ajax({
url: VIDEOS_UPLOAD_SERVICE_URL,
method: 'POST',
contentType: 'application/json',
headers: {
Authorization: 'Bearer ' + accessToken,
'x-upload-content-length': file.size,
'x-upload-content-type': file.type
},
data: JSON.stringify(metadata)
}).done(function (data, textStatus, jqXHR) {
...DO SOMETHING
}
})
I get an Error 405 Method not allowed, but a few days/weeks ago everything works fine :-(
The VIDEOS_UPLOAD_SERVICE_URL is https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet
But if I call the ajax Request to fetch the categories that works fine...
Related
I'm trying to do API request to a private API (hosted Itop), there's an example in the doc with JQuery Ajax but I did all my others calls with fetch and I wanted to do this one too but I don't get the right response.
The fetch method returns me a code 200 but with HTML instead of JSON (with a totally different content from the jQuery AJAX JSON one).
Here's the 2 functions:
// Code made by myself - don't get the right response
fetch(url, {
method: "POST",
headers: {
"accept": "application/json",
},
mode: "cors",
accept: "application/json, text/javascript, *!/!*; q=0.01",
body: JSON.stringify({
auth_user: user,
auth_pwd: password,
json_data: JSON.stringify({
"operation": "list_operations",
})
})
})
.then(response => response.json())
.then(data => {
console.log(data);
})
// Code from the doc example - working
$.ajax({
type: "POST",
url: url,
dataType: "json",
data: {
auth_user: user,
auth_pwd: password,
json_data: JSON.stringify({
"operation": "list_operations"
})
},
crossDomain: 'true'
})
.then(
function(data, textStatus, jqXHR) {
console.log(data);
console.log(textStatus);
console.log(jqXHR);
},
function(jqXHR, textStatus, errorThrown) {
console.debug(jqXHR);
console.log("ERROR !!\n" +
"status: " + textStatus + " (" + jqXHR.status + ")\n" +
"error: " + errorThrown);
}
);
In jQuery.ajax, when you pass data an object:
it will be encoded as application/x-www-form-urlencoded data
jQuery will include a Content-Type: application/x-www-form-urlencoded header
In fetch, you are passing body a string of JSON and fetch will be including a Content-Type: text/plain header.
So:
You are passing the wrong encoding of data
You are passing a Content-Type that doesn't match what the server expects or what your data actually is
Pass fetch a URLSearchParams object.
This will be encoded with the correct format and fetch will infer the correct content type from it.
I am currently using postman to make an API request that pushes body data. I can get this to work either using "x-www-form-urlencoded" or "raw". See examples below:
I'm trying to convert this to an ajax javascript request but unsure on how to format the body/data text. Here is my script:
$.ajax({
type: 'POST',
url: 'https://login.microsoftonline.com/***/oauth2/token',
headers: {
"Content-Type": "application/json"
},
data: {
" grant_type=client_credentials
&client_id=***
&client_secret=***
&resource=https://analysis.windows.net/powerbi/api "
},
success: (data) => {
console.log(data.token)
},
error: (data) => {
console.log('rr', data)
}
});
Any help would be appreciated
There's a mismatch here as you're setting the Content-Type header to JSON, yet you're sending form-urlencoded. You need to use one or the other consistently.
If you want to explicitly use JSON, do this:
$.ajax({
type: 'POST',
url: 'https://login.microsoftonline.com/***/oauth2/token',
contentType: 'application/json', // shorter than setting the headers directly, but does the same thing
data: JSON.stringify({
grant_type: 'client_credentials',
client_id: '***',
client_secret: '***'
resource: 'https://analysis.windows.net/powerbi/api'
}),
success: data => {
console.log(data.token)
},
error: (xhr, textStatus, error) => {
console.log('rr', error)
}
});
If you want to use a form-urlencoded string, do this:
$.ajax({
type: 'POST',
url: 'https://login.microsoftonline.com/***/oauth2/token',
data: 'grant_type=client_credentials&client_id=***&client_secret=***&resource=https://analysis.windows.net/powerbi/api',
success: data => {
console.log(data.token)
},
error: (xhr, textStatus, error) => {
console.log('rr', error)
}
});
Note in the above examples that the first argument to the error handler is not the request or response data as your example seems to expect. I've amended that part to accept the correct arguments.
Well, I send POST request to server-side to get data (xls). I use fiddler and see binary in response, but Chrome tells me there is a Network Error 0x800c0007.
Server side based on WCF. Here I take HttpResponse, copy Stream from Excel to response.OutputStream, and say response.flush(). It goes without errors.
I see response with 200 Code. But chrome does not react to data.
Probably I use wrong header or need to set up Ajax in different way.
$.ajax({
url: url,
method: 'POST',
type: 'POST',
data: JSON.stringify({ 'options': options }),
contentType: 'application/json;charset=utf-8',
accepts: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
dataType: 'json',
mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
headers: {
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"IF-MATCH": '*',
"X-HTTP-Method": "POST",
'mimeType': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'content-type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
},
error: function (data) {
console.log("error: ", data);
a.resolve(null);
},
success: function (data, status, headers, config) {
if (data != null) {
a.resolve(data);
}
}
});
return a.promise();
This is my last attempt.
Is it possible to send to server data by Post and have a response with binary?
I want to create new contact in google contact with google api with javascript and jquery, i can retrieve contacts with this sample :
jQuery.ajax({
url: "https://www.google.com/m8/feeds/contacts/default/full?alt=json&" +
"access_token=xs55.CjDOA8lRTs8567657567vXXXX&" +
"max-results=100&" +
"v=3.0",
headers: {
'Authorization': "Bearer xs55.CjDOA8lRTs8567657567vXXXX",
'Content-Type': 'application/json'
},
method: "GET",
dataType: 'jsonp',
success: function (data) {
console.log(data)
},
error: function (data) {
console.log('error: ');
console.log(data);
console.log(data.status);
}
})
Now i want to POST data and create or update items in google document there is always error ! :(
For example in this code "Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://XXX' is therefore not allowed access. The response had HTTP status code 405." error happened :
jQuery.ajax({
url: "https://www.google.com/m8/feeds/contacts/default/full?alt=json&" +
"access_token=xs55.CjDOA8lRTs8567657567vXXXX&" +
"max-results=100&" +
"v=3.0",
headers: {
'Authorization': "Bearer xs55.CjDOA8lRTs8567657567vXXXX",
'Content-Type': 'application/json'
},
method: "POST",
data: {title: "vvvv", phonenumber: "3333"},
//dataType: 'jsonp',
success: function (data) {
console.log(data)
},
error: function (data) {
console.log('error: ');
console.log(data);
console.log(data.status);
}
})
whitout jsonp option there is no way to work that this option used when want to GET something not POST it .
Can you try this:
data= {title: "vvvv", phonenumber: "3333"};
$.ajax({
type: "POST",
data :JSON.stringify(data),
url: "https://www.google.com/m8/feeds/contacts/default/full?alt=json&" +
"access_token=xs55.CjDOA8lRTs8567657567vXXXX&" +
//"max-results=100&" +
"v=3.0",
contentType: "application/json"
});
I have the following code as aprt of my .ajax section
success: function (data) {
alert("success");
},
error: function(xhr, ajaxOptions, thrownError) {
alert(xhr.status);
}
The first alert never runs, however the data is submitted correctly using the below:
data: JSON.stringify({ "solution": JSON.stringify(data) }), // Data is HTML
In fact, the second alert comes back with a status of 200 and everything through Google Chrome console looks fine.
Any idea? Full code:
var request = jQuery.ajax({
url: "/answers/"+content_id,
type: "POST",
data: JSON.stringify({ "solution": data }),
dataType: "json",
headers: {
Authorization: 'Basic XXX',
'X-HTTP-Method-Override': 'PATCH',
'Content-Type': 'application/json'
},
success: function (data) {
alert("success");
},
error: function(xhr, ajaxOptions, thrownError) {
alert(xhr.status);
}
});
The $.ajax function expects JSON data as response. If the response is not JSON, the error callback will be called. Please have a look at what you are sending out from server.
Please test some things:
Set type: "GET" instead of post. Look at this: GET OR POST
Headers data are string (name/value), and maybe your data encoding is utf8 so set
headers: {
'Authorization': 'Basic XXX', //high recommended
'X-HTTP-Method-Override': 'PATCH',
'Content-Type': "application/json; charset=utf-8" //low
},
Test another word instead of data to avoid conflict:
success: function (response)