CORS in Symfony 3 and Angular js

0

I made an Api with symfony, in which I define all the necessary headers. From Angular it is from where I consume the symfony api, the connection with my Api since angular is normal in all the requests, at no time I get an error by the CORS. The problem is that I have a part of the system that generates a large amount of bills, obviously makes certain heavy losses in my API and returns an answer. When generating the invoices in the system as they are more or less 2000 users, that therefore should generate 2000 invoices, to the minute and a half of executing the function of generating the invoices the error generates me:

  

Access to XMLHttpRequest at   ' link ' from origin   ' link ' has been blocked by CORS policy: No   'Access-Control-Allow-Origin' header is present on the requested   resource.

What I do not really know because it happens, because my API is with all the required headers and in other less heavy requests it works correctly from Angular, and I also did the test locally and it works correctly generating the almost 2000 invoices, but doing that The same test already on the server does not allow me to generate all the invoices, it only generates a certain amount and at the minute and a half I get that error.

Hopefully you can help me and tell me why on my server does not let generate requests for a longer time, because generally all those bills are generated in 4 min, but my server cuts the execution and I vote the error of the CORS, thank you very much .

    
asked by Diego A. Rosero Viveros 02.11.2018 в 16:28
source

1 answer

0

I'll give you a brief example, with different languages but that meet certain fundamental characteristics for a CORS problem:

If your error persists since the return of your server, it throws the following to what you mention:

  

Access to XMLHttpRequest at   ' link ' from origin   ' link ' has been blocked by CORS policy: No   'Access-Control-Allow-Origin' header is present on the requested   resource.

Explanation

the data that comes via AJAX must belong to the same domain. In the event that this is not the case, the application will not be able to load the data due to security limitations. The problems begin when we have applications that need access to that data but are not under the same domain. A very common example is a Mobile application packaged with PhoneGAP or in Ionic with Angular. So when you upload your app to a server it will respond correctly, it is only a limitation when you are working locally (this as an example in an environment similar to the one mentioned)

Solution

You can download an add-on for the browser either Chrome (it's easier) or another one of your preference. Look for it by that name CORS and it will help you, you just have to link the web address to which you make your request with ajax. And in this way you allow the connection between both parties.

Another possibility is that the document or file to which you point is coded to avoid security problems, and in this way can not be used from an external application (A domain X to a domain Y).

So, what you would do would be to use this type of headers so that you can receive the request correctly (If in your case you have access to such a file and of course, let it be the backend in PHP as an example):

<?php 
  //Permisos CORS

  //* es un comodin para desplegar la información a cualquier servidor que realice la peticion.
  header("Access-Control-Allow-Origin: *"); 

  //Debemos dar permisos de acuerdo a la llamada o metodo que necesites GET,POST,UPDATE.
  header('Access-Control-Allow-Methods', 'GET, POST');

  //Para formatos JSON se debe asignar el encabezado correspondiente.
  header("Content-Type: application/json");

 ?>
    
answered by 02.11.2018 в 16:34