I have searched the forums here for a while and am still at a loss. I am doing Basic Authentication with a predefined username and password.
I know that the username and password are correct because I can directly log in.
I have tried passing a "header", "username", "password" and as you see below using "beforeSend".
$.ajax({
type: "GET",
url: "https://vcc-na10.8x8.com/api/stats/groups.xml",
datatype: "xml",
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password)); },
error: function (){
alert('cant connect');
},
success: function (){
alert('connected');
}
});
I think you need to use the headers parameter to pass authentication. Try this:
$.ajax({
type: "GET",
url: "https://vcc-na10.8x8.com/api/stats/groups.xml",
datatype: "xml",
headers: {
"Authorization": "Basic " + btoa(username + ":" + password)
},
error: function() {
alert('cant connect');
},
success: function() {
alert('connected');
}
});
Try sending the username and password directly in the ajax config.
$.ajax({
type: "GET",
username: username,
password: password,
...
Related
I am trying to send a request using ajax to a server which is protected by basic authentication with the following code:
$('document').ready(function(){
$.ajax({
url: "https://somesite.com/somefolder",
type: "POST",
dataType: 'jsonp',
beforeSend: function(xhr) {
xhr.setRequestHeader ("Authorization", "Basic " + btoa('myusername' + ":" + 'mypassword'));
},
success: function(data){
console.log('SUCCESS!');
},
error: function () {
console.log("error");
}
});
});
So I provide the credentials in the beforeSend so my expectation would be that there would be no credential popup from the browser since I already provided the credentials but unfortunately when i run this code I get the popup to enter my credentials. I want to the code to provide these credentials.
Similar to this question, example using headers:
$.ajax({
url: "https://somesite.com/somefolder",
type: "POST",
dataType: 'jsonp',
headers: {"Authorization": "Basic " + btoa('myusername' + ":" + 'mypassword')},
success: function(data){
console.log('SUCCESS!');
},
error: function () {
console.log("error");
}
});
I'm trying to use AJAX to request a token. This will only be used on localhost to make the request. I've got the code:
$.ajax({
url: "TOKEN URL HERE",
beforeSend: function(xhr) {
xhr.setRequestHeader("grant_type", "client_credentials");
xhr.setRequestHeader("client_id", "ENTER CLIENT ID");
xhr.setRequestHeader("client_secret", "ENTER CLIENT SECRET");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Accept", "application/json");
},
dataType: "json",
//content-Type: "application/json",
type: "POST",
success: function(response) {
token = response.access_token;
expiresIn = response.expires_in;
},
error: function(errorThrown) {
alert(errorThrown.error);
}
});
However, it doesn't work. Is this the correct approach or have I got the parameters incorrect? (Or is OAuth2 token request just not possible with AJAX/JQuery/JS)
The answer of Piotr P wasn't working for me. The client I am getting the tokens from did not accept the client id and credentials when send as part of the data. I had to use basic authentication instead. This is the ajax call that worked for me.
$.ajax({
"type": "POST",
"url": "https://some.domain.com/oath/token",
"headers": {
"Accept": "application/json",
"Authorization": "Basic " + btoa(username + ":" + password)
},
"data": {
"grant_type": "client_credentials"
},
"success": function(response) {
token = response.access_token;
expiresIn = response.expires_in;
},
"error": function(errorThrown) {
alert(JSON.stringify(errorThrown.error()));
}
});
Are sure, that grant_type, client_id, client_secret should be passed by headers instead of payload?
Try to remove them from headers (left accept, content-type only), and then add "data" property with rest of params.
Something like that:
$.ajax({
url: "TOKEN URL HERE",
beforeSend: function(xhr) {
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Accept", "application/json");
},
dataType: "json",
data: {
client_id: "ENTER CLIENT ID",
client_secret: "ENTER CLIENT SECRET",
grant_type: "client_credentials"
},
type: "POST",
success: function(response) {
token = response.access_token;
expiresIn = response.expires_in;
},
error: function(errorThrown) {
alert(errorThrown.error);
}
});
I am trying to append extra information to the jsonrequest that is sent from my application to DialogFlow. I am aware of the possibility to send data calling an intent by triggering its event from Sending Parameters in a Query Request, and I am using that method already for other functionality.
Basically, I am trying to add a value with the userID, and I need to retrieve that value in DialogFlow. I am keepeing track of the session in php, so I am using phpto get ID value. I have tried to do the following in the ajaxrequest:
$.ajax({
type: "POST",
url: baseUrl + "query?v=20150910",
contentType: "application/json; charset=utf-8",
dataType: "json",
headers: {
"Authorization": "Bearer " + accessToken
},
//This folllowing line is the modified part:
data: JSON.stringify({
query: text,
lang: "en",
sessionId: "somerandomthing",
userId: "100"
}),
success: function(data) {
},
error: function() {
setResponse("Internal Server Error");
}
});
And this one as well:
$.ajax({
type: "POST",
url: baseUrl + "query?v=20150910",
contentType: "application/json; charset=utf-8",
dataType: "json",
headers: {
"Authorization": "Bearer " + accessToken
},
//This folllowing line is the modified part:
data: JSON.stringify({
query: text,
lang: "en",
sessionId: "somerandomthing",
userId: "100",
parameters: {
userId: "100"
}
}),
success: function(data) {
},
error: function() {
setResponse("Internal Server Error");
}
});
I need that value in the request to process some data in the back-end based on it. If anyone knows how to do it, or has suggestions for a better approach that would be much appreciated.
There are discussion posts related to this in the DialogFlow Forums, but there is still no resolution. The feature might not be available, or it is not documented Link 1, Link 2, Link 3.
I solved this issue with the help of #Svetlana from Dialogflow. The original post is Here, and here is an example from a JavaScript application with back-end in Node.js:
You would format your request like:
$.ajax({
type: "POST",
url: baseUrl + "query?v=20150910",
contentType: "application/json; charset=utf-8",
dataType: "json",
headers: {
"Authorization": "Bearer " + accessToken
},
data: JSON.stringify({originalRequest: {data: {exampleMessage: 'Test'}}, query: text, lang: 'en', sessionId: 'somerandomthing'}),
success: function (data) {
},
error: function () {
setResponse("Internal Server Error");
}
});
and you would access the value in the webhook with:
var exampleMessage = req.body.originalRequest.data.exampleMessage;
or
var exampleMessage = req.body.originalRequest.data['exampleMessage'];
Hope this helps.
I am trying to send a DELETE request to a tomcat server using AJAX.
I want the request to use a basic auth. I can't really get there if the password contains special characters.
The configuration of the system is:
jquery 1.10.2
tomcat 7.0.47
Before asking I try to follow what I found in:
How to use Basic Auth with jQuery and AJAX?
How can you encode a string to Base64 in JavaScript?
Javascript Ajax call using password with special characters
The Tomcat configuration is:
</tomcat-users>
<role rolename="authUser"/>
<user username="username" password="password" roles="authUser"/>
</tomcat-users>
I made an ajax request using jquery
$.ajax({
type: "DELETE",
url: "http://localhost:8080/TestAuthentication",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
username: "username",
password: "password",
success: function(){
alert("Data Deleted");
}
});
With this configuration everything works.
If I put special characters in the configuration file of tomcat I'm no more able to make correct request.
</tomcat-users>
<role rolename="authUser"/>
<user username="username" password="AeHm#%%9Rt\'JzX^|A'N" roles="authUser"/>
</tomcat-users>
I try with two ajax settings
$.ajax({
type: "DELETE",
url: "http://localhost:8080/TestAuthentication",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
username: "username",
password: "AeHm#%%9Rt\'JzX^|A'N",
success: function(){
alert("Data Deleted");
}
});
$.ajax({
type: "DELETE",
url: "http://localhost:8080/TestAuthentication",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
username: encodeURIComponent("username"),
password: encodeURIComponent("AeHm#%%9Rt\'JzX^|A'N"),
success: function(){
alert("Data Deleted");
}
});
with this two configurations I see the same request.
So I try a more older approach
$.ajax({
type: "DELETE",
url: "http://localhost:8080/TestAuthentication",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
beforeSend: function(xhr) {
xhr.setRequestHeader("Authentication", "Basic " + btoa(unescape(encodeURIComponent("username" + ":" + "AeHm#%%9Rt\'JzX^|A'N"))));
xhr.setRequestHeader("Authorization", "Basic " + btoa(unescape(encodeURIComponent("username" + ":" + "AeHm#%%9Rt\'JzX^|A'N"))));
},
success: function(){
alert("Data Deleted");
}
});
With this request I always get the error: 401 (Unauthorized)
But usign a c# client I'm able to authenticate the request:
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(#"http://localhost:8080/TestAuthentication");
request.Credentials = new NetworkCredential("username", #"AeHm#%%9Rt\'JzX^|A'N");
....
And I'm also able to access to URL if I put "username" and "AeHm#%%9Rt\'JzX^|A'N" on alert show me from the browser.
So it seems to be a problem of the request I made.
Please, could someone help me?
Finally I have undestand the problem was: the escape
Writing:
AeHm#%%9Rt\'JzX^|A'N
wrong output: AeHm#%%9Rt'JzX^|A'N =>so I lost the "\" character
Writing the password escaping the "\" character
AeHm#%%9Rt\\'JzX^|A'N
correct output: AeHm#%%9Rt\'JzX^|A'N
Here the working code:
$.ajax({
type: "DELETE",
url: "http://localhost:8080/TestAuthentication",
beforeSend: function(xhr) {
//May need to use "Authorization" instead
xhr.setRequestHeader("Authentication", "Basic " + btoa(unescape(encodeURIComponent("username" + ":" + "AeHm#%%9Rt\\'JzX^|A'N"))));
xhr.setRequestHeader("Authorization", "Basic " + btoa(unescape(encodeURIComponent("username" + ":" + "AeHm#%%9Rt\\'JzX^|A'N"))));
},
success: function(){
alert("Data Deleted");
}
});
I am trying to add header to ajax request.
I use setRequestHeader but it do nothing. I can look in firefox that request headers doesnot contains Authentication! What i am doing wrong?
And how to add header?
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://test/test.svc/news",
dataType: "json",
beforeSend : function(xhr) {
xhr.setRequestHeader("Authentication", "Basic " + Base64.encode(username + ":" + password))
},
success: function(data) {
alert('Success');
},
error: function() {
alert("Loading data error...");
}
});
}
Try username and password instead
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://test/test.svc/news",
dataType: "json",
username: 'username', /* not user */
password: 'pass',
success: function(data) {
alert('Success');
},
error: function() {
alert("Loading data error...");
}
});
}
username -
A username to be used in response to an HTTP access authentication request