java - Angular $http.get always get ERROR consuming local Restful Service -


i have error , getting confuse, have created simple java ee 7 project using jersey.

i returning class in rest rervice:

@xmlrootelement public class locationdtox {      private long id;             private string tittle;         private string description;             private long parent;      //getter , setters... 

and in service class have:

@path("/location") public class locationservice {      @get     @consumes(mediatype.application_json)     @produces(mediatype.application_json)     @path("/findlocation")     public locationdtox findlocation() {         system.out.println("findlocation");         try {             locationdtox x = new locationdtox();             x.setdescription("description");             x.setid(0l);             x.setparent(null);             x.settittle("tittle ...");             return x;         } catch (exception ex) {             logger.getlogger(locationservice.class.getname()).log(level.severe, null, ex);         }         return null;     }      } 

i 100% sure rest working , if put in browser:

http://localhost:8080/bireportl-war/rest/location/findlocation

i json string:

{"description":"description","id":0,"tittle":"tittle ..."}

the deal in angular code, code calling rest service angular getting executed giving me error part:

app.controller('questionscontroller', function ($scope, $http) {     //var url = "http://localhost:8080/bireportl-war/rest/location/findlocation";     //var url = "http://www.w3schools.com/angular/customers.php";     var url = "http://127.0.0.1:8080/bireportl-war/json.json";     $http.get(url)         .success(             function (data, status, headers, config) {                 alert("success");                })         .error(function(data, status, headers) {           alert('repos status ' + status + ' --- headers : ' + headers);         })         .finally(             function() {         }); }); 

i have comments local url dummy json file can access browser, , same result error, weird thing tried rest public json file:

http://www.w3schools.com/angular/customers.php

and success !! don't know why, doing or have wrong, mean when try local rest service, see getting called in logs, fact, angular client getting error.

thanks in advance !

i using: *glassfish v4 *angular

well, cors issue put rest below, here solution:

@consumes(mediatype.application_json)     @produces(mediatype.application_json)     @path("/findlocation")     public response findlocation() {         system.out.println("findlocation");         try {             locationdtox x = new locationdtox();             x.setdescription("description");             x.setid(0l);             x.setparent(null);             x.settittle("tittle ...");              return response.ok()                     .entity(x)                     .header("access-control-allow-origin", "*")                     .header("access-control-allow-methods", "get, post, delete, put")                     .build();         } catch (exception ex) {             logger.getlogger(locationservice.class.getname()).log(level.severe, null, ex);         }         return null;     }    

if angularjs accessing local rest api, fact you're running in browser on different port, counts different origin, per rules of cors (separate port means separate origin).

two pages have same origin if protocol, port (if 1 specified), , host same both pages.

for access-control-allow-origin response header, either set via *, or specify alternate ports explicitly. has fact browser (and angularjs) attempting play rules, can find on mdn's page on same origin policy.

these "rules" don't apply when load resource directly in browser, origin (page browser loading from) same port, you're loading just resource, @ origin (plus port).

[edit]

the cors standards included adherence response headers, such access-control-allow-origin , access-control-allow-methods.

references:

[/edit]


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -