i can't conect my server web2py restful with angular Ajax, but if i set the url in my browser it's fine it work , but i cant in angular ajax =(
Link of Angular
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js"></script>
<script src="https://code.angularjs.org/1.3.16/angular-route.min.js"></script>
Mi code in my server web2py (Controller)
@request.restful()
def api():
response["Access-Control-Allow-Origin"] = "*"
response["Access-Control-Allow-Methods"] = "POST, GET, OPTIONS"
response["Access-Control-Max-Age"] = "1000"
response["Access-Control-Allow-Headers"] = "*"
response.view = 'generic.json'
def GET():
print "asdasds"
return dict(content="JAJAJAJAJA")
return dict(GET=GET)
and mi AJax in angular url= Address:port/nameProject/controller/action
app.controller('controlVentas', function( $http) {
var app = this;
app.CargarLlave=function(){
var respuesta=$http.get("http://127.0.0.1:8000/Hoteles/ControlMSR/api");
respuesta.success(function(data){
alert( "OK");
});
respuesta.error(function(data, status, headers, config){
alert( "NOOOOO");
});
}
});
error of angular
XMLHttpRequest cannot load http://127.0.0.1:8000/Hoteles/ControlMSR/api. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:57734' is therefore not allowed access.
The proper way to set response headers is via response.headers
, not directly on the response
object. For example:
response.headers["Access-Control-Allow-Origin"] = "*"
This is my final Code, thanks all =)
Code in mi Web2Py
@request.restful()
def api():
response.view = 'generic.json'
response.headers["Access-Control-Allow-Origin"] = '*'
response.headers['Access-Control-Max-Age'] = 86400
response.headers['Access-Control-Allow-Headers'] = '*'
response.headers['Access-Control-Allow-Methods'] = '*'
response.headers['Access-Control-Allow-Credentials'] = 'true'
def GET():
names = ['1dddedede', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
return dict(person=names)
return locals()
Code in Angular
app.controller('controlVentas', function( $http) {
var app = this;
app.CargarLlave=function(){
var respuesta=$http.get("http://127.0.0.1:8000/Hoteles/ControlMSR/api");
respuesta.success(function(data){
alert( "OK");
});
respuesta.error(function(data, status, headers, config){
alert( "NOOOOO");
});
}
});