Im new in Angular 2 framework and I try tu understand Http object.
Im following the tutorial here:
http://www.joshmorony.com/using-http-to-fetch-remote-data-from-a-server-in-ionic-2/
Its perfect to retrieve JSON file from a server that provide this kind of data but I want to retrieve the HTML content into a variable. I would like to have all the html file and parse it to get some informations.
When I modify the URL in the example by a website like this:
this.http.request('https://www.google.ca/' ).map(res => res.text()).subscribe(data => {
this.webPage = data.toString();
console.log("data:" + data );
});
I got this error:
EXCEPTION: Response with status: 0 for URL: null
Do I use the good API?
What im doing wrong?
Thanks,
Pat
This is a CORS problem.
The full error is:
XMLHttpRequest cannot load https://www.google.ca/.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'https://run.plnkr.co' is therefore not allowed access.
EXCEPTION: Response with status: 0 for URL: null
You cannot request 'https://www.google.ca/' because: 1) when you make a http request in your app, the origin is already defined as your host, could be 'localhost' or your site's host. 2) 'www.google.ca' is another origin, and it has no 'Access-Control-Allow-Origin' set, or you site's host is not in their 'Access-Control-Allow-Origin' list.
If you want the html of 'www.google.ca', you need a proxy server, and the steps are: 1) in your ng2 app, you send a request to proxy server and ask it to do ... 2) you proxy server request 'www.google.ca' and send the html to your ng2 app.
When the proxy server request 'www.google.ca', the origin is defined as 'www.google.ca'. So the proxy server can get the html.
Related
I want to get api_kye and I use moqui framework in backend , use axios in react js project :
axios.get(SERVER_URL + '/rest/api_key', {
headers: {
Authorization: "Basic " + btoa(username + ":" + password) ,
'Content-Type' : 'application/x-www-form-urlencoded' ,
},
}).then(response => {})
then , when requested the below error happened :
Access to XMLHttpRequest at '...' from origin '...' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header has a value '...' that is not equal to the supplied origin.
This is not a reactJS error, this is a problem with your backend code, you need to look at the Moqui docs to see how you can allow the origin you are calling from to access your API
Enable CORS in the server side
Let's head back to our server's app.js file.
app.get('/cors', (req, res) => {
res.set('Access-Control-Allow-Origin', '*');
res.send({ "msg": "This has CORS enabled 🎈" })
});
Also check the redirect URL in server side
Refer this link
when ever you send an HTTP request to an API the server response should include your domain in response header otherwise chrome will raise this error: "Access to XMLhttprequest has blocked for origin ...."
so first of all make sure that your domain is included in server code.
if it's included already then the reason could be that server can't process your response correctly. maybe it crashed so the response is corrupted and chrome can't find your domain in response header and it raises that error
So i have a solution for this but you may or may not be able to build it.
CORS is a security feature, you should be receiving requests thru your backend and not the browser directly in general...
IF the data is not sensitive and you want to open an endpoint to the world without getting CORS errors you can do one of two things
Set the CORS headers from the server side. You'll need to understand HTTP requests and headers. you set these from the server. enabling cross origin with * will work.
Build a proxy. I've done this in AWS API gateway, I'll link another post. works good. basically AWS will act as your back end and take the response with CORS blocked. you will then proxy the request and strip the CORS header. When you call the api you will actually call AWS which calls the API, then AWS will pass the response back to you with CORS enabled.
Angular No 'Access-Control-Allow-Origin'
just follow these steps and it will work.
I am trying to hit below api and requires basic auth username and password and method allowed in this is only get
dispatch(requestBegin());
let apiPath = `xxxx/TEST/appservice/api/app/10/10000127201901`;
return fetch(apiPath, {
method: 'get',
headers : {
"contentType":"application/x-www-form-urlencoded",
"Authorization" : 'Basic '+btoa('xxx:xxx'),
},
})
.then((response) => {
dispatch(getEventsEnds(json));
})
.catch((error) => {
dispatch(getEventsEnds());
});
The error loged in console :
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:2200' is therefore not allowed
access. The response had HTTP status code 405. If an opaque response
serves your needs, set the request's mode to 'no-cors' to fetch the
resource with CORS disabled.
OPTIONS http://xxx/appservice/api/app/10/10000127201901 405 (Method
Not Allowed)
Can anyone please eplain when i m trying to hit get api then why is it showing options
That means your API server does not accept CORS request or requests originating from localhost.
Check out your API documentation but my guess is you won't be able to interact directly with it from your web app.
Your best bet is to use a proxy, you can develop one yourself or use something like node-http-proxy to proxy your API calls. (There are php or python equivalents)
The proxy server will be able to issue the requests and will then forward them to your app.
Suggested further reading: type understanding CORS on google and read more about it.
For local development, check out:
https://github.com/Rob--W/cors-anywhere
This issue occurs when the client api URL and server URL don't match, including the port number. In this case you need to enable your service for CORS which is cross origin resource sharing.
use npm install cors
refer this[refer][1]
I am using Google matrix api in angularjs like below code :
var params = {origins: $scope.getrest.origin, destinations: addr, key: 'AIzaSyAfkiSErIwa6AGNYmig'};
return $http.get('https://maps.googleapis.com/maps/api/distancematrix/json', {params: params}).then(function (res) {
});
but it gives me error show in image and i also include js file and also try all solution from google but not find any proper solutions.
Error Image
Make the request to googleapis server side to avoid the X domain origin rule (e.g. if you are using Java in your backend make the request there) then your front end code can easily access that data
I try to send http request with $http (angular) with this code:
$http({
method: 'GET',
url: 'http://192.168.0.17:9000',
header: {'Access-Control-Allow-Origin': "*"},
}).then(getEventsSuccess, getEventsError);
But this doesn't work and I have in the web console this error:
XMLHttpRequest cannot load http://192.168.0.17:9000/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.
Do you have a solution ?
You see this error due to a security mechanism implemented in your browser, called Same Origin Policy.
Basically, it is caused since your webpage tries to access a resource which resides on a server that is on a different Host, Port or Scheme (HTTP / HTTPS / file etc) than the webpage itself.
In order to solve this issue, you can do one of the following:
Serve your Webpage from the server that you are trying to access. If your webpage URL will be http://192.168.0.17:9000/X.html, your request should be successful and the error will disappear.
Add a special header to the response sent from your server, called Access-Control-Allow-Origin.
Read more here:
https://en.wikipedia.org/wiki/Same-origin_policy
https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
I'm trying to call the zillow api webservice url from angular js.
The following is my source code :
$http.get('http://www.zillow.com/webservice/GetZestimate.htm?zws-id=<ZWSID>&zpid=48749425').success(function(response) {
alert(response) ;
console.log(response);
});
It is giving the following error :
XMLHttpRequest cannot load http://www.zillow.com/webservice/GetZestimate.htm?zws-id=<ZWSID>&zpid=48749425.No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8080' is therefore not allowed access
Please anyone help me.
Thanks in advance.!
From this Link :
Zillow API Error:"No 'Access-Control-Allow-Origin' header is present"
Zillow doesn't support a JavaScript API so we would need to create own server-side service that queries it (forwards the query) and sits on the same domain as our HTML page.
The following post gives the full code using java
How to send data to an API call and get it back, using zillow.com API