I am using Angular 2 to make a webpage. When I load the page, I use OnInit to run the following method (with generic names substituted):
getAllObjects(): Promise<object[]>{
return this.http.get(this.getAllObjectsUrl).toPromise().then(response => response.json().data).catch(this.handleError);
}
I've verified in my browser that the getAllObjects url does indeed return an array of object in JSON format. Here is the url if it is helpful:
private getAllObjectsUrl : 'http://99.240.124.235:7060/REST/rest/companyService/getAllCompanies.json/';
However, this method triggers the handleError catch and the browser's debugging log shows GET localhost:3000/null 404 NOT FOUND (I am using an npm server to run it, hence the localhost).
I do not believe it is a CORS issue because I downloaded the Chrome CORS plugin and other API calls have worked. It is only this particular API url that is causing a problem, which I find strange as my other API calls work and they follow the exact same format (except a different url).
I thought maybe the appComponent wasn't allowed an OnInit, but I replaced getAllObjects() with a different working API call, and I didn't receive this error.
I am completely stuck and any help would be appreciated (could this error be because of the web API, not the front end?).
getAllObjectsUrl doesn't contain the url because you are using colon(:) instead of equal sign(=)
Colon indicates the type of the variable, whereas equal sign initializes it.
change this
private getAllObjectsUrl : 'http://99.240.124.235:7060/REST/rest/companyService/getAllCompanies.json/';
for this
private getAllObjectsUrl = 'http://99.240.124.235:7060/REST/rest/companyService/getAllCompanies.json/';
Related
Our goal is to redirect http://localhost:4200/oauth_callback to http://localhost:4200/#/oauth_callback
We have a multi-language site in angular 11 and are using HashLocationStrategy. We can't change this at the moment. We are also moving to an OAuth authentication. I'm told OAuth doesn't allow for a fragment hash in the callback url. Our redirect url '/oauth_callback' currently shows up as an error - "This localhost page can’t be found". I'm guessing it's attempting to find oauth_callback.html and there isn't anything there.
Is it possible to add an interceptor to our app that looks for /oauth_callback in the url and redirects it to /#/oauth_callback? Or is there an easier way to do this redirect in app-routing.module.ts. I'm fine with any solution as long as it redirects properly without using a .htaccess file.
I'm not very familiar with interceptors but I did find this gist (https://gist.github.com/AntwaneB/5322d8292867863d866e9f1071535be0) and tried to get this to work however, I'm getting an error when using .catch, EmptyObservable(), and _throw() with our current request.interceptor.ts file. When I add import 'rxjs/add/operator/catch'; the error is still there for some reason and I get "Property 'catch' does not exist on type 'Observable<HttpEvent>'."
I've done a bunch of Googling and am mostly seeing request to take the hash out of url's completely but we are wanting to add it in the url. Appreciate any help or guidance.
I've got a new API from the backend team in a new project, when I call the api it returns "you need to enable java...", whereas I had used Postman for another project before... is it related to api, server or something else?
I don't think that POSTMAN is capable of executing JavaScript in its console.
Try doing the same in the web browser it will work (You won't see this error message).
I spent some times pondering on this trepidation.. and then suddenly i realized what was going on..
the endpoint does not exist, it could be a misspelling
not in the same directory as you expect it to be,
try adding or removing "/" at the beginning of the url, particularly if you don't specify the hostname, i.e. fetch('getusername') is different from fetch('/getusername') .
. This acceptable in development but NOT when already deployed, it points to different path.
the endpoint may be working fine in the Development,
but somewhere within in the Production/Staging, it generated some exception.
I updated Postman and now it works. I'm not sure if it was because of the update or the restart.
I had this problem with a project built using the new template in Visual Studio 2022 for a React app with .NET Core.
In my case I was only getting the response "You need to enable JavaScript to run this app" with calls to a new controller I added. Calls to the built-in WeatherForecastController were working just fine. My new controller was configured the same as the built-in controller so I could not figure out why this was happening. It has to do with how this project template creates both a React app and a back-end API both accessible on the same port. There's a setupProxy.js file that defines routes that should be forwarded to the API. All other routes are redirected to index.html. This is actually what was happening in my case, because my new controller had not been added to setupProxy.js the middleware was redirecting the request to index.html, and because it came from Postman rather than a browser the message regarding enabling JavaScript is displayed.
The solution is that each controller must be explicitly mapped in setupProxy.js or else it won't be proxied correctly. After making this change it worked perfectly in Postman as well as fetch calls from the React app.
const context = [
"/weatherforecast", // built-in controller than comes with the project template in VS2022
"/recaptcha" // controller I created (this line must be added)
];
While calling the REST API with the postman, if you miss the end-point, then also this issue will come, add the end-point to the URL and check
What worked for me was to turn-off / deselect the user-agent header field under request
I am trying to capture query string parameters for analytics purpose using javascript. I did some searching and found that BMP can be used to do it but i am unable to find ample examples to implement. Could anyone point me in the right direction.
EDIT 1:
I used below code using browsermob-proxy to get har file but i get ERROR: browsermob-proxy returned error when i run it . I use selenium with it.
getHarFile() {
const proxy = browsermb.Proxy;
const pr = new proxy({host:"0.0.0.0",port:4444});
pr.doHAR("http://www.cnn.com/", (err,data) => {
if (err) {
logger.debug('ERROR: ' + err);
} else {
fs.writeFileSync('ua.com.har', data, 'utf8');
logger.debug("#HAR CREATED#");
}
})
}
So since I´m not quite sure of your scope I will throw you some ideas:
1. Fixing browsermob-proxy
You should change the host and proxy of the browsermob-proxy. Change the host to 127.0.0.1 and the port with any random number (4444 its ok). Then, make sure your browser run in that host and proxy by changing the browser settings.
2. Using plain javascript
2.1 Get current page query string
You can get the query string using location.search. If you are using some BDD framework with selenium, it is possible to execute javascript code and retrieve the result. You should always add a return to your code in order to recieve the response in your BDD test.
2.2 Using Performance API
You can access to all the network information within performance api. If you need to get the current page url you can use the following code:
performance.getEntriesByType("navigation")
This will return all the current navigation events and information.
If you want to get some information of the calls that the page made you can access it using:
performance.getEntriesByType("resource")
This will return all the calls made by your site. You have to loop over it searching the resource you want to find.
In all the ways, there is no way to get the value and key of query string as in the network tab. You have to separate it manually with a function, you can use the code provided here to get the value of a key.
My suggestion is to create your personal extension for Google Chrome, and developing an extension you can access few more apis that are not available by default in the console.
For example you will have this object in order to inspect the network tab:
chrome.devtools.network
Here two links you may find useful:
https://developer.chrome.com/extensions/devtools
https://developer.chrome.com/extensions/devtools_network
I hope it helps
I was finally able to do it using the s object available on chrome console. The url with encoded query string was available as s.rb object in chrome console. I just decoded it and extracted the query string parameters.
As part of a project whose aim is to notably improve the visual side of a SharePoint Online site, I'm a bit stuck. On the home page in the left banner, users want to see the number of unread messages they have in Office365.
I created an area in the master page to put the result in. I thought the Rest API used to do this :
$.ajax ({
type: "GET",
url: " https://outlook.office365.com/ews/odata/Me/Folders/Inbox",
dataType : "json",
success : function (resp) {
// count unread messages
},
error : function (e) {
alert (' Error121212 :' + JSON.stringify (e));
}
})
Unfortunately I get an error like cross domain. I tried with JSONP but it does not work either (uncaught syntax error unexpected token).
Can you please tell me if this is a good practice? I feel that it anyways I must find a technic for authentication. (In the case of JSONP I have a popup that asks me authentication and then problem occurs on callback apparently)...
I want to avoid developing a type requiring a typical deployment Wsp...
Thank you in advance for your help.
Your URL for the ajax request seems incorrect. The URL for getting the inbox messages via the API is: https://outlook.office365.com/api/v1.0/me/folders/inbox/messages
Once you get the response, you can count the number of objects with the IsRead property set to false using a simple for loop and display that count.
The issue here is related to CORS and how browsers refuse to handle cross-domain requests. To get around this, typically you would either
Change the response header on the remote server - not an option here
Use some sort of proxy to handle the requests - here's where SharePoint apps come in.
I know you stipulated that you want to avoid using a WSP style deployment but there simply isn't a way around it, you have to use the SharePoint App Model
This article goes a long way to answer your question, but for completion the basic steps are as follows
Create a SharePoint hosted app in Visual Studio
In the App Manifest, you need to define the trust relationship with the remote host (in this case the host of outlook.office365.com) using the AppManifest section
Use SP.RequestExecutor.executor to make the request on your behalf
I am using the google auth but keep getting an origin mismatch. The project I am working has sub domains that are generated by the user. So for example there can be:
john.example.com
henry.example.com
larry.example.com
In my app settings I have one of my origins being http://*.example.com but I get an origin mismatch. Is there a way to solve this? Btw my code looks like this:
gapi.auth.authorize({
client_id : 'xxxxx.apps.googleusercontent.com',
scope : ['https://www.googleapis.com/auth/plus.me',
state: 'http://henry.example.com',
'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile'],
immediate : false
}, function(result) {
if (result != null) {
gapi.client.load('oath2', 'v2', function() {
console.log(gapi.client);
gapi.client.oauth2.userinfo.get().execute(function(resp) {
console.log(resp);
});
});
}
});
Hooray for useful yet unnecessary workarounds (thanks for complicating yourself into a corner Google)....
I was using Google Drive using the javascript api to open up the file picker, retrieve the file info/url and then download it using curl to my server. Once I finally realized that all my wildcard domains would have to be registered, I about had a stroke.
What I do now is the following (this is my use case, cater it to yours as you need to)
On the page that you are on, create an onclick event to open up a new window in a specific domain (https://googledrive.example.com/oauth/index.php?unique_token={some unique token}).
On the new popup I did all my google drive authentication, had a button to click which opened the file picker, then retrieved at least the metadata that I needed from the file. Then I stored the token (primary key), access_token, downloadurl and filename in my database (MySQL).
Back on step one's page, I created a setTimeout() loop that would run an ajax call every second with that same unique_token to check when it had been entered in the database. Once it finds it, I kill the loop and then retrieve the contents and do with them as I will (in this case I uploaded them through a separate upload script that uses curl to fetch the file).
This is obviously not the best method for handling this, but it's better than entering each and every subdomain into googles cloud console. I bet you can probably do this with googles server side oauth libraries they use, but my use case was a little complicated and I was cranky cause I was frustrated at the past 4 days I've spent on a silly little integration with google.
Wildcard origins are not supported, same for redirect URIs.
The fact that you can register a wildcard origin is a bug.
You can use the state parameter, but be very careful with that, make sure you don't create an open redirector (an endpoint that can redirect to any arbitrary URL).