Error when accessing localhost api in chrome net::ERR_INSECURE_RESPONSE

匿名 (未验证) 提交于 2019-12-03 01:58:03

问题:

I was trying to access api running in localhost using angular $resourses, chrome console gives me error saying ERR_INSECURE_RESPONSE.

I tried disabling web security in chrome. still same error. here is the angular factory that i used. How can I bypass this error and test my app.

    ImpactPortal.factory('apiFactory', function ($resource) {     return $resource('https://localhost:8443/mifosng-provider/api/v1/client_impact_portal', {}, {         query: {             method: 'GET',             params: {},             isArray: true         }     }) }); 

回答1:

You must authenticate first and then send each request along with the auth token.

I am using RestAngular so my settings might look a little different from what you are working on.

This will go in your application config :-

RestangularProvider.setDefaultHeaders({ 'X-Mifos-Platform-TenantId': 'default' }); 

and something like this will go in your controller/service

var login = Restangular.all('authentication?username=mifos&password=password').post().then(function(user) {    console.log(user);  }, function() {   console.log("There was an error saving");  }); 


回答2:

Enabling CORS in Angular.js

var myApp = angular.module('myApp', [     'myAppApiService']);  myApp.config(['$httpProvider', function($httpProvider) {         $httpProvider.defaults.useXDomain = true;         delete $httpProvider.defaults.headers.common['X-Requested-With'];     } ]); 

A server supporting CORS must respond to requests with several access control headers:

Access-Control-Allow-Origin: "*" 

By default, CORS requests are not made with cookies. If the server includes this header, then we can send cookies along with our request by setting the withCredentials option to true.

Access-Control-Allow-Credentials (optional) 

If we set the withCredentials option in our request to true, but the server does not respond with this header, then the request will fail and vice versa.



回答3:

Only try this:

Go to https://[your-domain].com and then chrome block you with the famous page:

Your connection is not private

So go down to ADVANVCED and then proceed.

Your certificate is probably self-signed.

Remember to do that for each Chrome session.



回答4:

Error 501 (net::ERR_INSECURE_RESPONSE) - 501 Not Implemented

The server either does not recognize the request method, or it lacks the ability to fulfill the request. Usually this implies future availability (e.g., a new feature of a web-service API).

Can you confirm that curl request is working fine

curl -k --user damien@email.com:password https://localhost:8443/mifosng-provider/api/v1/client_impact_portal 

If it is working fine:

ImpactPortal.factory('apiFactory', function ($resource) {     return $resource('https://localhost:8443/mifosng-provider/api/v1/client_impact_portal', {}, {         query: {             method: 'JSONP',             params: {},             isArray: true         }     }) }); 

Try following this tutorial that consume an external API: http://www.toptal.com/angular-js/a-step-by-step-guide-to-your-first-angularjs-app



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!