Can't access another http resource from ionic app - angularjs

I am working on Ionic framework , i am trying to access an Restful API using $http service in my Ionic framework i ma getting the following error in console.
XMLHttpRequest cannot load https://apiexample.com/token/ 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:8100' is therefore not allowed access. The response had HTTP status code 405.
Anyone know how to access that Rest full API from my Angular Js using $http service ?
Any help will be appreciated, Thanks in advance :D

This is a setting that you need to enable on the server side. Ensure you have the right CORS settings on the server side.

Related

Allowing CORS only for my domain?

I have an AngularJs website and when I am trying to post data then when I am opening my website without using www then I am getting
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource
Otherwise, I am not getting any error.
I tried to search and found that I should implement CORS on my backend which is in NodeJs but can anyone please tell me how can I only implement CORS Headers such that for both www and without, it would work but for any other domain trying to access my API must result in preflight error.
I am trying to do this because I read here which-security-risks-do-cors-imply that allowing all domains can increase security overhead for my website which I do not want.
Thanks.
I'm afraid this is not something you can tweak just in your client-side code. In order for cross-origin requests to work, you need to set an http response header: it's the server, who serves the resource, who will need the change, not the client side code from angularJs.
I believe that you should update your question stating what your server side language is and how are you handling http requests in the server side. As far as I know, just adding a header like:
Access-Control-Allow-Origin: http://client.domain.com
In your responses will do the trick. Where client.domain.com is the domain of your client, angularJs application.

how to allow django rest api to response requests made from different hosts?

i developed a web api using djangorestframework but when i am consuming api using angularJS, browser shows error
XMLHttpRequest cannot load http://127.0.0.1:8000/webapi/orders. Response > to preflight request doesn't pass access control check: No 'Access-
Control-Allow-Origin' header is present on the requested resource. Origin > 'http://127.0.0.1:37749' is therefore not allowed access.
angularJS app is hosted on 127.0.0.1:37749/ and
django api is hosted on 127.0.0.1:8000/
how to configure settings.py to solve this problem?
please help!!!
You are having an issue with Cross Origin Resource Sharing.
See http://www.django-rest-framework.org/topics/ajax-csrf-cors/#cors for solution. In particular django-cors-headers is what you'll want to fix that issue.

How to fix this Access-Control-Allow-Headers error

I'm using angularJS for POST from localhost to some API. but I got this error message:
XMLHttpRequest cannot load "...API link..." Request header field authorization is not allowed by Access-Control-Allow-Headers in preflight response.
I can post to the API when I use "Allow-Control-Allow-Origin" chrome extension. But this is just temporary, This way only works when I use this extension on my borwser. But my users got this problem when they use in their own browser.
How to fix this from client side.
I don't think you can fix this issue client side. The API server controls who can access what (using the Allow-Control-Allow-Origin header) so you have to change it there.
The CORS (Cross-Origin Resource Sharing) Request should be enabled in your server i mean in your api like header("Access-Control-Allow-Origin: *");
, otherwise if you still want to access the request from another domain (localhost) if api host is different , in your js code try to use jsonp
here is the documentation
https://docs.angularjs.org/api/ng/service/$http#jsonp

Google Cloud Endpoints v2 enable CORS

I'm trying to support cross-origin resource sharing (CORS) calls on my API written using Google Cloud Endpoints Framework v2 in Python, but I am not able to do it.
According to the documentation, CORS is enabled by default, but I can't make any calls from a different origin using Javascript, I get this:
XMLHttpRequest cannot load https://myapp.appspot.com/_ah/api/apiname/v1/events. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access. The response had HTTP status code 500.
I've tried adding response.headers like this SO question does, but it's not working either.
Does anyone know how I can enable CORS in my API?
Thanks!
The SO link you posted mentions that the CORS headers need to be sent by the server.
You have to send the http header: "Access-Control-Allow-Origin" with value of "*" or "your client domain".
You can either send this header from the Extensible Serviceble Proxy (ESP) which is part of the Google Cloud Endpoint, or you can send the http header from your server side api script.
I would recommend sending the header from the webserver, since the ESP may cache the api responses.

Unable to get the xml response from zillow api webservice which is called from angularjs

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

Resources