Cannot send HTTP request while debugging - nativescript

My applications works fine when i run it by tns run ios, but when i try to debug it with tns debug ios it doesn't work well.
When i do an HTTP request to my back-end it won't be received in the back-end and an error wil occur.
"this.dispatchMessage is not a function. (In 'this.dispatchMessage', 'this.dispatchMessage' is undefined)"
This is the code i am using:
http.request({
url: config.baseUrl,
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
content: data
}).then(function (response) {
// Success
}, function (e) {
// error occur
});

Related

Cypress - test redirect

I have a local endpoint that checks if I'm logged in and if yes, it generates token and redirects me to another API endpoint. I want to test the redirection however, I'm ending with the following error:
cy.visit() failed trying to load:
http://getmantaportal.test/authorize/
The response we received from your web server was:
> 403: Forbidden
This was considered a failure because the status code was not 2xx.
This http request was redirected '1' time to:
- 302: https://example.com/api/?token=eyJ0eXAiOiJKV1QiL
Here is my code:
it('Redirect to Manta Edge if already logged in.', function () {
// Login first.
// [...]
// Go to the endpoint and get redirected immediately.
cy.visit('/authorize/')
cy.url().should('contain', '/api/?token=')
})
Thanks a lot in the advance.
Use nested cy.request calls, you'll need to know all redirects you will get to be authenticated.
See the hypothetical example below.
cy.request({
method: "POST",
url: `YOUR FIRST URL`,
headers: {},
body: {
username: YOUR_USERNAME,
password: YOUR_PASSWORD
},
}).then(function (response) {
var body = response.body;
cy.request({
method: "GET",
url: `YOUR SECOND URL`,
auth: {
bearer: body.access_token,
},
}).then(function (response) {
cy.request({
method: "POST",
url: `YOUR THIRD URL IF EXISTS`,
auth: {
bearer: body.access_token,
},
}).then(function (response) {
response.headers
// If necessary, get the cookies to finish the authentication
});
});

trying to build http adapter from ajax request

i'm trying to build http adapter with token authorization form ajax request but get 401 error
Status Code:strong text
401 Unauthorized
missing_authorization
$.ajax({
type: "POST",
url: "https://abcd",
data: JSON.stringify({ "template": 1 }),
headers: { "Authorization": "xxxx", "Accept": "application/json",
"Content-Type": "application/json" }
});
function My_adapter() {
path = '/xxx';
var input = {
method : 'post',
path : path,
returnedContentType : 'json',
headers: {'Content-type':'application/json',
'Accept':'application/json', 'Authorization':'Token XXXXX'},
parameters: JSON.stringify({ "template": 1 }),
};
var result=WL.Server.invokeHttp(input);
return result;
}
tnx for your help,
sahar
The error message you see is expected. The client side code you posted shows that you are attempting to invoke MFP server outside of MFP client SDK ( jQuery ajax call). This call does not carry all the required information to the server and server sends the "missing_authorization" message as a result.
If you wish to invoke an adapter, use WLResourceRequest API provided by MFP client SDK. This takes care of handling the authentication handshake with MFP server. More details on the API usage here.

How to send push notifications in Chrome(Progressive Web Apps)

Please explain how to do push notifications with XHR and Javascript. or is there any other way to send push notifications in progressive web apps. I have created curl command and when i execute it in my terminal push notification sent but how to do it on button click?
Here is my cURL command:-
curl --header "Authorization: key=AIzaSxUdg" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send -d "{\"registration_ids\":[\"cxA-dUj8BTs:APAvGlCYW\"]}"
This is what i have tried :-
function send()
{
navigator.serviceWorker.ready
.then(function(registration) {
registration.pushManager.getSubscription()
.then(function (subscription) {
curlCommand(subscription);
$.ajax({
url: "https://android.googleapis.com/gcm/send",
headers: {
Authorization: "key=AIzaSxUdg",
},
contentType: "application/json",
data: JSON.stringify({
"registration_ids": [endpoint]
}),
xhrFields: {
withCredentials: true
},
crossDomain: true,
type:"push",
dataType: 'json'
})
.done(function() {
alert('done');
})
.fail(function() {
alert('err');// Error
});
})
})
}
But it shows error -----
XMLHttpRequest cannot load https://android.googleapis.com/gcm/send. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8880' is therefore not allowed access..
Google API is intended to be used from a server so it is not including CORS headers.
As you are performing a cross origin XHR (from your domain to Google's domain), the User Agent makes a preflight request in search of the CORS headers that tell your client is authorized to perform operations.
To do this you need to make a request to your server (i.e. a POST on /notifications/send) and your server should then execute the cURL request to GCM.
This will work:
function send()
{
navigator.serviceWorker.ready
.then(function(registration) {
registration.pushManager.getSubscription()
.then(function (subscription) {
curlCommand(subscription);
$.ajax({
url: "https://cors-anywhere.herokuapp.com/https://android.googleapis.com/gcm/send",
headers: {
Authorization: "key=AIzaSxUdg",
},
contentType: "application/json",
data: JSON.stringify({
"registration_ids": [endpoint]
}),
xhrFields: {
withCredentials: true
},
crossDomain: true,
type:"push",
dataType: 'json'
})
.done(function() {
alert('done');
})
.fail(function() {
alert('err');// Error
});
})
})
}

JQuery - Ajax - Set Request Header - DocuSign

Afternoon!
I a trying to send a get request usng jquery/ajax and I need to set headers. When I try setting the headers using Chrome's REST app, it woks fine and returns what I need.
I am watcing the headers in Firebug hen I run this code:
$.ajax({
url: urlLoginCall,
type: 'GET',
headers: {'Accept': 'application/json'},
success: function(r){
alert("success");
},
error: function(){
alert("failure");
}
});
The above works fine and I get a failed response, since I am not including the authorization info in the headers. But it at least shows me something in Firebug that I am setting the header Accept.
When I try to add the X-DocuSign-Authentication header, i get the failed alert, but nothing comes up in Firebug:
$.ajax({
url: urlLoginCall,
type: 'GET',
headers: {'X-DocuSign-Authentication': jsonLoginCall},
success: function(r){
alert("success");
},
error: function(){
alert("failure");
}
});
Any ideas would b helpful. Thanks!

Request headers not passed when proxying with Apache HTTPD server

I have set up a proxy on Apache HTTP server for making the cross domain Ajax call. The call is happening but is returning a response indicating that the API key passed as a request header is invalid.
Can you make cross domain Ajax calls using Apache web server proxy? If so what am I doing wrong. ?
The proxy setting is as follows:
ProxyPass /api-temp/* http://api.temp.com/
The code to make the Ajax call is as follows:
var imageNamespace = {
imageUrls: [ ],
url: '/api-temp/v1/data/cat/42736286',
test: function() {
alert(234);
},
getImages: function() {
$.ajax({
type: 'GET',
dataType: 'json',
cache: true,
headers: {'API_KEY': 'xxxxxxxxxxxxxxxxxxxxxxxxx'},
url: imageNamespace.url,
success: function () {
alert('success:', success);
},
error: function (error) {
alert('ERROR:', error);
},
complete: function () {
alert('complete');
}
});
},
}
Here is the response in the browser:
The URL it is invoking is as follows: http://api.temp.com/v1/data/cat/42736286
Please enter valid API Key
I am passing the key in the request header but it is not being picked up. Any insight on how to resolve this will be helpful.

Resources