AWS Cognito Authorize API call Using Javascript - javascript

I have a API which Authenticate by AWS cognito service. i wanted to call it using ajax .i used below code for that.
$.ajax({
url: 'https://XXXXXXXXX.execute-api.us-east-2.amazonaws.com/XXXXXXXX/setusername',
type: 'POST',
dataType: 'json',
data: {},
header :{'Authorization':result.getIdToken().getJwtToken()},
success: function (data, textStatus, xhr) {
console.log(data);
},
error: function (xhr, textStatus, errorThrown) {
console.log('Error in Operation');
}
});
but it gives me below error when i execute it.(Note: when i remove cognito Auth from API gateway this working fine)
Console Error
and below is my integration response

try this library to sign the call to your API Lambda method https://github.com/mhart/aws4
or are you using a custom auth?

Related

Redirecting an iFrame with POST data from jQuery

I have not found a solution for this on the internet, just some that don't work.
This is the problem:
function goto_checkout(){
jQuery.ajax({
type: 'POST',
dataType: "JSON",
url: checkouturl,
success: function (data, textStatus, XMLHttpRequest) {
jQuery.redirect(payments_url, data);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
}
The AJAX call returns a JSON object. Right now, I am then redirecting to a third-party website submitting my JSON object as POST data.
I don't want a redirect anymore, but I want my iFrame with id iFrame1 to go to the same website while submitting the same POST data there.
Please help me i am desperate

connection to TFS-Api from Javascript Client

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.

call server side inherited C# web method from javascript

Please Help me,
I want to call a server side function in page1 from page2 using java script.
For example in Home.aspx.cs i have read() webmethod and i want to call this method from Call.aspx's Javascript function.
You need to create an ajax call:
$.ajax({
url: "linkToOtherPage",
method: "POST", // or GET, I don't know what you need
data: dataToPass,
success: function (response) { },
error: function(jqXHR, textStatus, errorThrown) { }
});

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

Use Javascript to connect to WCF Web Service

Does anyone know how to use Javascript to connect to a WCF Web Service?
All I need at this point is to actually connect to the web service, and be notified that the connection was successful.
Does anyone know how I can do this?
If your WCF service is within the same domain you might use the below function that would perform the call
function TestingWCFRestWithJson() {
$.ajax({
url: "http://localhost/Service/JSONService.svc/GetDate",
dataType: "json",
type: "GET",
success: function (data, textStatus, jqXHR) {
// perform a success processing
},
error: function (jqXHR, textStatus, errorThrown) {
// show error to the user about the failure to invoke the service
},
complete: function (jqXHR, textStatus) {//any process that needs to be done once the service call is compelte
}
});
}
If your WCF service is in some other domain other than your calling applications domain then you would need to perform a JSONP call as shown below:
function TestingWCFRestWithJsonp() {
$.ajax({
url: "http://domain.com/Service/JSONPService.svc/GetDate",
dataType: "jsonp",
type: "GET",
timeout: 10000,
jsonpCallback: "MyCallback",
success: function (data, textStatus, jqXHR) {
},
error: function (jqXHR, textStatus, errorThrown) {
},
complete: function (jqXHR, textStatus) {
}
});
}
function MyCallback(data) {
alert(data);
}
When a JSONP call is performed using JQuery's $.ajax the complete/success/error methods are not fired rather a callback method as shown is fired which needs to be handled by the WCF service. There is a attribute "crossDomainScriptAccessEnabled" provided by the WCF framework that identifies if the request is a JSONP call and writes the content back to the stream to invoke the callback function with data. This is available on the binding element as shown below:
<webHttpBinding>
<binding name="defaultRestJsonp" crossDomainScriptAccessEnabled="true">
<readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="64" maxNameTableCharCount="2147483647" />
<security mode="None" />
</binding>
</webHttpBinding>
Given you'd properly written/configured your/the WCF service you should be able to load the following url:
http://somedomain.com/somewcfservice.svc/jsdebug
and call the exposed methods.

Categories