connection to TFS-Api from Javascript Client - javascript

I am trying to connect a JavaScript client with a TFS 2015 API to get some information and save it in my SQL Database, but I am having challenges establishing this connection with JavaScript. They are using Windows authorization.
I used this code but it didn't work:
$.ajax({
url: 'https://tfs........',
type: 'GET',
dataType: 'json',
xhrFields: {
withCredentials: true
}
})
.done(function (data) {
console.log(data);
})
.fail(function (jqXHR, textStatus, errorThrown) {
console.log(textStatus);
});
Can anyone help me please?

Refer to these 2 threads(one, two), check if the Access-Control-Allow-Origin is a wildcard like '*'. This is not allowed.
Update:
Refer to this case who have similar issue with you: connecting to TFS using windows auth in electron app.
You could create a login in page, and store username and password in cookies. Then use httpntlm to do this request.

Related

Is it possible to use GET & POST inside one $.ajax for login?

Good day,
We are building a project that uses WordPress for the marketing(normal pages, blogs, etc) and angular for the more functional side of this project. My current issue is we use the wordpress side for registration, subscribing to newsletter, and logging in. Newsletter and registration is now okay but I have a problem with the login. I'm using a custom bootstrap form, and use ajax. I'm given an api to get the login token first, then include that token in post for logging in.
This is currently what I got in my scripts.js:
$('#login-button').click(function(event) {
event.preventDefault();
// extracting the token
$.get("https://sample-dummy-url/Account/Loginwp", function(data, status){
// setting the token to loginToken variable to include in logging in
loginToken = $(data).find("input[name='__RequestVerificationToken']").val();
// checking if token is extracted
console.log(loginToken);
});
// I know this will be empty outside of the function of getting the token, but this is my current idea
console.log(loginToken);
// setting the variable to pass for logging in
let login = {
Username: $('#loginUsername').val(),
Password: $('#loginPassword').val(),
button: 'login',
// this is where I supposedly assign the extracted token but I know this is empty
__RequestVerificationToken: loginToken,
RememberLogin: $('#loginRememberMe').is(":checked"),
}
// ajax for logging in
$.ajax({
url: 'https://sample-dummy-url/Account/Login',
headers: {
'Access-Control-Allow-Origin': '*',
'Accept': 'application/json',
'Content-Type': 'application/json; charset=UTF-8'
},
type: 'POST',
dataType: 'json',
data: JSON.stringify(login),
success: function(msg, textStatus, jqXHR) {
console.log("logged in");
},
error: function(msg, textStatus, jqXHR) {
console.log(msg);
console.log(textStatus);
console.log(jqXHR);
},
});
});
My idea was only to invoke the getting of token when login button is clicked, then extract the token to a variable so I could include it in the login object so that when I use another ajax to post for login. I know this might be not the best/ideal/practical implementation, but I am truly open for suggestions & enlightenment of the proper way of doing it.
Thank you for any help.

connecting to TFS using windows auth in electron app

i'm building an electron app that connect to a TFS server and retrieve some data from it.
They use Windows credential to access TFS , how can i connect to TFS using Windows credential through chrome or node.js ?
i tried this code but give me 401 : unauthorized
$.ajax({
url: 'https://tfs.myserver.com:8090/tfs/IKM.TPC.Projects/MR/_workitems',
type: 'GET',
dataType: 'json',
xhrFields: {
withCredentials: true
}
})
.done(function (data) {
$('#result').text(data);
})
.fail(function (jqXHR, textStatus, errorThrown) {
$('#result').text(textStatus);
});
Simple steps:
Create a login page to let user login with username and password
Store username and password in a variable, cookie or local storage
Send the request by using httpntml with username, password, domain

jQuery and Ajax - cannot POST

I am trying to login to a website using a known username and password and to get some data displayed from the site for a specific user account on that website. I am using jQuery and Ajax for this purpose. This is my code:
$.ajax({
async: false,
cache: false,
type: 'POST',
dataType: 'json', // json...just for example sake
data: ({
'login_username': username,
'secretkey': password
}),
url: 'https://mail.someserver.com/src/redirect.php',
success: function (data) {
alert("SUCCESS!")
if (data === '1') { // server returns a "1" for success
// success!
// do whatever you need to do
} else {
// fail!
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
// something went wrong with the request
alert("Failed!");
}
});
I've already made my search around the web and I know that browsers do not permit cross server ajax calls to prevent security issues, but I've already tried to use "jsonp" as dataType to no avail :(
So, what am I doing wrong?
Be sure that your url is not breaking the same origin policy -- that is, the request coming from the client cannot request data from a server from a different domain (there are exceptions to this rule, namingly CORS, but that requires that you make changes to the server/application you're talking to).
The solution to your problem would be to make the request from some server-side script, then in turn having your client application query that script, based on the same machine that's serving the application to the web.
My fault isn't at the code above, my fault was that in my manifest file (I am building a Google Chrome extension) I didn't have set proper permissions (https://*).
Sorry for the frustration!

Retrieving user_timeline from Twitter in XML

I'm writing a simple app in HTML and Javascript. I'm trying to retrieve my user_timeline via jQuery's .ajax() method. The problem is that I want to retrieve my timeline in XML but I'm keep failing with this simple task. Here's my code:
$.ajax({
type: 'GET',
dataType: 'xml',
url: 'http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=stepanheller',
success: function(data, textStatus, XMLHttpRequest) {
console.log(data);
},
error: function(req, textStatus, error) {
console.log('error: '+textStatus);
}
});
Weird thing is that when I try exactly the same thing but with JSON instead of XML then the script works.
$.ajax({
type: 'GET',
dataType: 'jsonp',
url: 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=stepanheller',
success: function(data, textStatus, XMLHttpRequest) {
console.log(data);
},
error: function(req, textStatus, error) {
console.log('error: '+textStatus);
}
});
Can you give me some hints what I'm doing wrong with the request? I know that I'm using old version of API but I won't deal with OAuth right now. Thanks.
It is generally impossible to send a Cross-domain ajax request. It's the general rule.
JsonP is the classic way to work around this limitation, and there is no equivalent for Xml according to my knowledge. Depending on your browser compatibility constraints, you can use XHR2 to achieve this.
Otherwise, the only solution is to set up a server proxy.
Client --Ajax--> Your server --HttpRequest--> Twitter

Facebook access_token error in Blackberry

In my Blackberry app using phonegap, I have Facebook share feature and I am doing this using phonegap's childbrowser. After I login to Facebook, access_token is to be requeted. For this I am using the following code :
$.ajax({
url:'https://graph.facebook.com/oauth/access_token?client_id='+my_client_id+'&client_secret='+my_secret+'&redirect_uri=http://www.facebook.com/connect/login_success.html',
data: {},
dataType: 'text',
type: 'POST',
success: function(data, status){
alert(22);
// We store our token in a localStorage Item called facebook_token
localStorage.setItem(facebook_token, data.split("=")[1]);
window.plugins.childBrowser.close();
app.init();
},
error: function(error) {
alert("fail"+error['message']);
window.plugins.childBrowser.close();
}
});
But the control is not going inside the success section instead it is going inside error section. What may be the problem. When I try to find the error using "alert("fail"+error['message']);", it is coming as undefined. Please help me to resolve this.

Categories