Difficulty accessing url with angular post

Asked

Viewed 128 times

0

I’m passing a url and some data via angular post... However, the following warning appear on Chrome console:

Cross origin requests are only supported for Protocol schemes: http, data, Chrome, Chrome-Extension, https, Chrome-Extension-Resource.

Follow me angular code:

$http.post("localhost:8765/api/v1/users/oauth.json?token="+t1+"&redirect_url=localhost:8765/api/v1/users", data).success(function(responde){
            console.log(responde);
        });

What does this warning mean?

2 answers

1

You are probably running this file without a local server using the protocol file:\\.

Additionally, in the excerpt:

$http.post("localhost:8765/api/v1/[...]

Angular is trying to infer the protocol based on its reference call, which translates as:

$http.post("file:\\localhost:8765/api/v1/"[...]

How to deduce from the error message, file:\\ is not a protocol supported for CORS.

To solve the problem serve the reference page via a local web server.

  • True, when I went to answer that question I didn’t take that into consideration. Very good!

  • I’ve already changed it to "HTTP://localhost:8765/api/v1/users/...". I’m not using file:\

  • No 'Access-Control-Allow-Origin' header is present on the requested Resource. Origin 'http://localhost:8080' is therefore not allowed access.

  • @Gustavosevero in the case he refers to his client. Which address in the top bar of your browser?

  • This HTTP://localhost:8080/weset/www/#/login

0

There is a rule that is a 'a.com.br' domain page can not make requests in the 'b.com.br' domain this is basically a security rule to avoid using the exclusive services of an application in another location.

I suppose you have 2 servers one for the angular project *(with html, js, css...) and the other for the backend (in this case php)

In this case the php server has to be configured to enable cross orign.

I found this link that can help you https://enable-cors.org/server.html

In php:

<?php
header("Access-Control-Allow-Origin: *");
?>

*(very careful when using header in php)

  • This line:header("Access-Control-Allow-Origin: *"); I have to put in ALL PHP files?

  • No 'Access-Control-Allow-Origin' header is present on the requested Resource. Origin 'http://localhost:8080' is therefore not allowed access.

  • I would put in 1° file to be loaded by the system, *(I don’t know exactly cake) but usually it is an index.php that instantiates the framework and gives the "run()"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.