HTTP post in Angular

1

I'm trying to access a web service that gets in the Header "Device" and Body "User and Password", it returns me json in this format:

{
  "IdUsuario": 2,
  "Usuario": "Fulanu",
  "Token": "1f7b87d7" 
}

Trying to do this in angle, follow my code:

$scope.logar = function (){
  var data = ({Usuario: "usu",Senha:"sen"});
  var config = {
            headers : {
                'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;',
                'data': {Device:"1"}
            }
        }

  $http.post("URL", data,config).success(function(data, status){
    console.log(data);
  });
};

This is returning me following error:

  

XMLHttpRequest can not load. Response to preflight request   does not pass access control check: No 'Access-Control-Allow-Origin'   header is present on the requested resource. Origin 'null' is   therefore not allowed access.

    
asked by anonymous 03.11.2016 / 22:57

3 answers

1

Thanks, guys,

Although it did not work out the ways you suggested.

I decided on my own server (MVC WebApi) with the following code in WebApiConfig

 var cors = new EnableCorsAttribute(origins:"*", headers:"*", methods:"*");
        config.EnableCors(cors);

Also in the submission my date looks like this:

 var data = "Usuario=fulanu&Senha=senha";
    
07.11.2016 / 18:23
1

You are trying to access a service from a local file. This is not possible with the same source policy unless you enable server-side access by sending Access-Control-Allow-Origin: * to headers .

  

In computing, same-source policy is an important concept in the web application security model. This policy allows scripts to run on pages that originate from the same site - a combination of schema, host name, and port number - to access the DOM from each other without specific restrictions, but prevents access to the DOM from different sites. 1] This policy also applies to XMLHttpRequests unless the server provides an Access-Control-Allow-Origin (CORS) header. Notably WebSockets are not subject to the same source policy.

However for development purposes in chrome you can start with the following parameters:

--allow-file-access-from-files --disable-web-security

In Windows it will look similar to the following:

C:\Users\SEU USUARIO\AppData\Local\Google\Chrome\Application\chrome.exe --allow-file-access-from-files --disable-web-security

Reference: CORS, Cordova, AngularJs $ http and file: // confusion

    
03.11.2016 / 22:59
1

Recently I went through the same problem while trying to get data from an external API that blocked my requests with the same error as yours. What I did to solve the problem was to create a configuration for all $ http services through a interceptor (which is a service that "intercepts" every request and applies the configuration before it is executed). See:

angular
.module('seuModulo')
.config('InterceptorConfig', function($httpProvider) {
    $httpProvider.defaults.useXDomain = true;
})

See if it solves your problem.

    
04.11.2016 / 00:06