0

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.
6
  • it's $http.get, not $http.GET. yes, case does matter here, and this is the angular get() function, not the GET method on the server that is being called. also, you aren't providing $http in your controller injections. try app.controller('controlVentas', ['$scope', '$http', function($scope, $http) { Commented Aug 27, 2017 at 22:58
  • Possible duplicate of Cannot read property 'get' of undefined with Angular Commented Aug 27, 2017 at 23:02
  • now evolution mi problem, one sec to update Commented Aug 27, 2017 at 23:17
  • in mi controller is called,method and it work, is actived but dont respond nothing. O.o Commented Aug 27, 2017 at 23:25
  • you need to have CORS. 127.0.0.1:8000 and 127.0.0.1:57734 are not the same site. try stackoverflow.com/questions/34621368/…, for example. Commented Aug 27, 2017 at 23:27

2 Answers 2

2

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"] = "*"
Sign up to request clarification or add additional context in comments.

Comments

0

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"); 
           });  
  } 
});    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.