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?
Related
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.
I have a js when i do a ajax call to one server. When i call to this service using localhost, works. But when i call to this service using the server where i upload it, its ERROR 500. The call return a json.
$.ajax({
url: "https://www.example.com/example",
dataType: "json",
data: {
'data': xml,
'message': message,
'customer_id': customer_id,
'subscr_id': subscr_id
},
type: 'POST',
success: function (devol) {
},
error: function (xhr, ajaxOptions, thrownError) {
alert("no ha entrado");
}
});
Try to update your code below:
var model = {
'data': xml,
'message': message,
'customer_id': customer_id,
'subscr_id': subscr_id
};
$.ajax({
url: "https://www.example.com/example",
type: 'POST',
dataType: 'json',
data: JSON.stringify(model),
//headers: { 'authorization': `Bearer ${token}` },
async: false,
processData: false,
contentType: "application/json",
error: function (err) {
},
success: function (data) {
}
});
It might be because of CORS (Cross-Origin Resource Sharing)... Usually, you cannot make calls to other domains from the browser unless the other domain you are making calls allows CORS to all sites or specific sites.
enter link description here
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)
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...